An interactive Ruby lesson
Hashes
A hash stores entries. Each entry maps a unique key to a value. Build on Variables to read and change values by key.
p displays an inspected value, with quotes around strings and curly braces around hashes. 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 a hash
Curly braces enclose a hash's entries. Commas separate them. In name: "Ana", name: writes the symbol key :name, paired with the string value "Ana". A symbol such as :name is a Ruby value often used as a label. {} creates an empty hash with no entries. Predict both output lines.
Show expected output
{name: "Ana", coins: 3}
{}Try it: Change "Ana" to "Mina". Predict which output line changes.
2. Read a value by key
Put a key inside square brackets to read its value. profile[:name] uses the symbol key :name. The key identifies an entry; it is not a position number. Predict both output lines.
Show expected output
"Ana" 3
Try it: Change the first profile[:name] to profile[:coins]. Predict both lines.
3. Check whether a key is present
For the hash literals in this lesson, reading a missing key with [] returns nil. A stored value can also be nil. .key? returns true if the key has an entry and false if it is absent. The question mark belongs to the method name. Predict all four lines.
Show expected output
nil nil true false
Try it: Change the stored nil to false. Predict which output line changes and whether :nickname is still present.
4. Replace an existing value
Assign a value to an existing key to replace its value. profile[:coins] = 5 changes the value for :coins. The :name entry stays the same. Predict the new coin value and the whole hash.
Show expected output
5
{name: "Ana", coins: 5}Try it: Assign 8 to :coins instead. Predict both output lines.
5. Add a new entry
Assigning to a key that is absent adds an entry. profile[:city] = "Cebu" adds the key :city and its value. Predict the city value and the whole hash.
Show expected output
"Cebu"
{name: "Ana", coins: 3, city: "Cebu"}Try it: Change "Cebu" to "Manila". Predict both output lines.
6. Check length and emptiness
.length counts entries. .empty? returns true for a hash with no entries and false otherwise. Predict all four lines.
Show expected output
2 false 0 true
Try it: Change profile 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
profilewithname: "Ana"andcoins: 3. - Change the value for
:coinsto5, then add:citywith the value"Cebu". - Use
pto display the values for:name,:coins, and:city, then the hash's length, in that order. - Change the city value to
"Manila". Predict which output line changes.
Show one solution and its output
profile = {name: "Ana", coins: 3}
profile[:coins] = 5
profile[:city] = "Cebu"
p(profile[:name])
p(profile[:coins])
p(profile[:city])
p(profile.length)Output:
"Ana" 5 "Cebu" 3
Ruby documentation
The Ruby 4.0 documentation covers hash syntax, reading values by key, and checking for a key. It also describes assigning values, length, and empty?.
Return to the Ruby skill tree to choose another topic.