Ref. WGR Chapter 9, Section 9.2, Collection handling with arrays
fruits = ["apple", "banana"]
a = [1, 2, 3]
a.push "four" #=> [1, 2, 3, "four"]
a.pop #=> "four"
a #=> [1, 2, 3]
a = [1, 2, 3]
a.push(4) #=> [1, 2, 3, 4]
a.shift #=> 1
a #=> [1, 2, 3]
a.unshift "zero" #=> ["zero", 1, 2, 3]
push
means "enqueue"; shift
means "dequeue"a = [1, 2, 3]
a.include?(2) #=> true
(technically an array is not a set because it doesn't enforce uniqueness)
first
and last
a[0] #=> 1
a.first #=> 1
a[a.size-1] #=> 3
a.last #=> 3
a = []
a << "x" #=> ["x"]
<<
(pronounced "shovel") does a push
-- adds an item to the end of an array -- DESTRUCTIVE!
a = []
a + ["x"] #=> ["x"]
+
does a concat
-- adds two arrays together and makes a new array -- safe!
concat
, not append
a = ["x", "y"]
a + "z" #=> can't convert String into Array
a + ["z"] #=> ["x", "y", "z"] (but a is unchanged)
a += ["z"] #=> ["x", "y", "z"] (and a is changed)
a = b = ["x"]
a += ["y"]
a #=> ["x", "y"]
b #=> ["x"]
size
isn't everythingsize
and length
are synonymsArrays are zero-indexed
fruit = ["apple", "banana", "cherry"]
fruit[0] #=> "apple"
fruit[2] #=> "cherry"
fruit[3] #=> nil
If you use 0-based indexing
Think of the index as pointing to the space between items
fruit = ["apple", "banana", "cherry", "date"]
negative indexes count from the back
fruit[-1] #=> "date"
fruit[-3] #=> "banana"
range indexes
fruit[1..3] #=> ["banana", "cherry", "date"]
fruit[1...3] #=> ["banana", "cherry"]
>> a = ["apple", "banana", "cherry"]
=> ["apple", "banana", "cherry"]
>> a.length
=> 3
>> a[5]
=> nil
>> a.size
=> 3
>> a
=> ["apple", "banana", "cherry"]
moral: getting past the end returns nil
, not error
>> a
=> ["apple", "banana", "cherry"]
>> a[9] = "jicama"
=> "jicama"
>> a.size
=> 10
>> a
=> ["apple", "banana", "cherry", nil, nil,
nil, nil, nil, nil, "jicama"]
moral: setting past the end autofills with nil
times_table = []
4.times do |x|
times_table[x] = []
4.times do |y|
times_table[x][y] = x * y
end
end
>> times_table
=> [[0, 0, 0, 0], [0, 1, 2, 3], [0, 2, 4, 6], [0, 3, 6, 9]]
>> times_table[2][3]
=> 6
>> times_table[2]
=> [0, 2, 4, 6]
/