0Pricing
Ruby Academy · Lesson

Creating and Reading Hashes

Key-value storage.

Creating and Reading Hashes is a free Ruby Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is a Hash?

A Hash stores key-value pairs. Each key maps to one value, like a dictionary. Keys are unique.

person = { name: "Ada", age: 36 }
puts person

Reading a Value

Read a value with square brackets and its key.

person = { name: "Ada", age: 36 }
puts person[:name]
puts person[:age]

Missing Keys Return nil

Asking for a key that does not exist returns nil rather than an error.

person = { name: "Ada" }
puts person[:email].inspect

Adding and Updating

Assign with brackets to add a new pair or update an existing key.

person = { name: "Ada" }
person[:age] = 36
person[:name] = "Grace"
puts person

String Keys

Keys do not have to be symbols. Strings, numbers, or any object can be a key. Be consistent so lookups match.

scores = { "math" => 90, "art" => 80 }
puts scores["math"]

An Empty Hash

Start empty with {} or Hash.new and fill it later.

cart = {}
cart[:apples] = 3
cart[:pears] = 5
puts cart

Counting Pairs

size or length tells how many pairs are in the hash. empty? checks for none.

h = { a: 1, b: 2, c: 3 }
puts h.size
puts h.empty?

Checking Keys and Values

Use key? (alias has_key?) and value? to test membership.

h = { name: "Ada" }
puts h.key?(:name)
puts h.value?("Ada")
puts h.key?(:age)

Getting All Keys and Values

keys and values return arrays of all keys or all values.

h = { a: 1, b: 2 }
puts h.keys.inspect
puts h.values.inspect

Deleting a Pair

delete removes a pair by key and returns the deleted value.

h = { a: 1, b: 2 }
removed = h.delete(:a)
puts removed
puts h

Nested Hashes

Values can be hashes too, building structured data.

user = { name: "Ada", address: { city: "London" } }
puts user[:address][:city]

Quick Check

Check your hash basics.

Recap

You can now use hashes:

  • create with { key: value } or {}
  • read and write with brackets
  • missing keys return nil
  • keys, values, size, key?, delete
  • nest hashes for structured data

Next: iterating over hashes.

h = { x: 10 }
h[:y] = 20
puts h.values.sum

Frequently asked questions

Is the “Creating and Reading Hashes” lesson free?

Yes — the full text of “Creating and Reading Hashes” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.

What will I learn in “Creating and Reading Hashes”?

Key-value storage. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Creating and Reading Hashes” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Symbols vs Strings
  2. Creating and Reading Hashes
  3. Hash Iteration
  4. Default Values and Merging
← Back to Ruby Academy