Hash Creation
In this page:
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
person = { name: "Alice", age: 30 }
old_style = { "name" => "Bob", "age" => 25 }
puts person.inspect
puts old_style.inspect
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: