Hashes
A hash maps keys to values. The { key: value } shorthand — using a bare word followed by a colon — is really building a Hash with Symbol keys; { name: "Ada" } is shorthand for { :name => "Ada" }. Symbol keys are idiomatic Ruby precisely because symbols are lightweight and immutable (lesson 10 covers exactly why).
main.rb
Try it yourself →person = { name: "Ada", age: 36 }
puts person[:name]
puts person[:age]
person.each do |key, value|
puts "#{key}: #{value}"
end
Output
Ada 36 name: Ada age: 36
each on a hash yields each key/value pair as two block parameters — another block, the same mechanism lesson 12 covers in full. Next: symbols — what :name actually is, and why it's a genuinely distinct type from a string with no real equivalent elsewhere on this site.
Practice exercise
Create capitals = { france: "Paris", japan: "Tokyo" }, then print the capital of Japan.