Lesson 7 of 15

Hashes

Hashes in Ruby

A hash is a collection of key-value pairs, similar to a dictionary. Keys are most often symbols (prefixed with :):

person = { name: "Alice", age: 30, city: "Paris" }
puts person[:name]   # Alice
puts person[:age]    # 30

You can also use strings as keys, but symbols are more common:

config = { "host" => "localhost", "port" => 5432 }
puts config["host"]  # localhost

Adding and Updating

person = { name: "Alice", age: 30 }
person[:city] = "Paris"     # add new key
person[:age] = 31           # update existing key

Common Methods

h = { a: 1, b: 2, c: 3 }
puts h.keys.inspect    # [:a, :b, :c]
puts h.values.inspect  # [1, 2, 3]
puts h.length          # 3
puts h.key?(:a)        # true

Your Task

Starting with person = { name: "Alice", age: 30, city: "Paris" }:

  1. Print person[:name]
  2. Print person[:age]
  3. Add key country with value "France"
  4. Print person.keys.length
  5. Print person[:country]
ruby.wasm loading...
Loading...
Click "Run" to execute your code.