← Back to Ruby Course | Chapter 5: Hashes | Lesson 1 of 7

Hash Creation

In this page:

  1. Hash Creation

Hash Creation

A Ruby hash stores key-value pairs, created with curly braces, { key => value } or the modern symbol-key shorthand { key: value }. Hashes are Ruby's dictionary/map type, letting you look up values by an arbitrary key rather than a numeric index. Keys are usually symbols or strings, though any object can serve as a key.

Note: The { key: value } shorthand is only valid when keys are symbols -- string keys still need the => (rocket) syntax.

Example: Hash Creation

markup
person = { name: "Alice", age: 30 }
old_style = { "name" => "Bob", "age" => 25 }

puts person.inspect
puts old_style.inspect
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.