An interactive Ruby lesson
Arrays
An array stores values in order. Each value is an element. Build on Variables to read elements by position and change an array.
p displays an inspected value, with quotes around strings and brackets around arrays. Predict the output, press Run, then open the expected output to check your answer. Edit the code and run it again. Reset restores that example; Stop ends a running example.
The first Run downloads Ruby into your browser. JavaScript and a network connection are needed to run examples. Each run starts fresh, so define every variable in the editor you run. The code and answers remain readable without JavaScript.
1. Create an array
Square brackets enclose an array's elements. Commas separate them. Here names holds three strings in the order written. [] creates an empty array with no elements. Predict both output lines.
Show expected output
["Ana", "Ben", "Cora"] []
Try it: Change "Cora" to "Dee". Predict which output line changes.
2. Read an element by index
An index is an element's position number. Ruby starts counting at 0: the first element has index 0, the second has index 1, and the third has index 2. names[0] reads the first element. Predict all output lines.
Show expected output
"Ana" "Ben" "Cora"
Try it: Change the first names[0] to names[2]. Predict the output before running.
3. Read the last element or a missing position
Negative indexes count from the end: -1 reads the last element. For this three-element array, -3 reads the first. Reading one index outside the array returns nil. Predict the results for -1, 3, and -4.
Show expected output
"Cora" nil nil
Try it: Change names[-4] to names[-3]. Predict which line now shows a string.
4. Replace an existing element
Assign a value to an existing index to replace that element. Here names[1] = "Mina" changes the second element. Predict the whole array and the value read at index 1.
Show expected output
["Ana", "Mina", "Cora"] "Mina"
Try it: Change the assignment to names[0] = "Mina". Keep p(names[1]). Predict both lines.
5. Append one value
<< adds one value to the end of an array. Predict the updated array and its last element.
Show expected output
["Ana", "Ben", "Cora"] "Cora"
Try it: Append "Dee" instead of "Cora". Predict both output lines.
6. Check length and emptiness
.length counts the elements. .empty? returns true for an array with no elements and false otherwise. The question mark is part of the method name. Ruby methods that answer a yes-or-no question often end in ?. Predict all four lines.
Show expected output
2 false 0 true
Try it: Change names to hold []. Predict its length and whether it is empty.
Practice on your own
Replace the code in the last editor with a program for these tasks. Predict the output, then run it.
- Create
nameswith"Ana"and"Ben", in that order. - Replace the first element with
"Mina", then append"Cora". - Use
pto display the whole array, the element at index2, and the array's length, in that order. - Change the appended value to
"Dee". Predict which output lines change.
Show one solution and its output
names = ["Ana", "Ben"] names[0] = "Mina" names << "Cora" p(names) p(names[2]) p(names.length)
Output:
["Mina", "Ben", "Cora"] "Cora" 3
Ruby documentation
The Ruby 4.0 documentation covers arrays, reading elements by index, and assigning elements. It also describes appending with <<, length, and empty?.
Return to the Ruby skill tree to choose another topic.