Arrays
Goals
Step 1
IRBfruits = ["kiwi", "strawberry", "plum"]IRBthings = [5, 'tree', 19.5] things.lengthIRBempty_array = [] empty_array.length
Step 2
IRBfruits[0] fruits[1] fruits[2]IRBfruits.first fruits.last
Step 3
IRB['salt'] + ['pepper']IRBfruits + ['mango'] fruits
Step 4
IRBfruits = ["kiwi", "strawberry", "plum"] fruits.push('apple') fruits.pop()
Explanation
length how long is this array (how many elements) first get the first element of the array (same as array[0]) last get the last element of the array (same as array[-1]) push add a new element to the end of the array pop remove (and return) the element at the end of the array IRB[].pop 3.0.pop
Further Reading
Ruby's documentation for Array
Next Step:
Go on to Hashes
Back to Working With Collections