Ruby arrays can be mixed with variables, numbers, etc. friends = Array[1,"karen","Oscar"]
friends = Array["Kevin","karen","Oscar"] puts friends[0]
"Kevin" puts friends[-1]
"Oscar" puts friends[-2]
"Karen" puts friends[0,2]
"Kevin" "Karen" puts friends[0,3]
"Kevin" "Karen" "Oscar"
friends = Array["Kevin","karen","Oscar"] friends[0] = "Dwight" puts friends[0]
Dwight
friends = Array.new
friends[0] 0 "Michael" friends[5] = "Holly"
puts friends
"Michael"
"Holly"
friends = Array["Kevin", "karen", "Oscar"] puts friends.length()
3
puts friends.include? "Karen"
True puts friends.include? "Karens"
False puts friends.reverse()
Oscar Karen Kevin friends = Array["Kevin", "karen", "Oscar","Andy"]
puts friends.sort()
Andy Karen Kevin Oscar
friends = Array["Kevin", 1, "Oscar","Andy"]
puts friends.sort()
error (Cannot sort because the type is different)
Recommended Posts