CHAPTER 5
Hashes
A hash in Ruby is a dictionary-style collection, also known as an associative array in other programming languages. Rather than using integer-based indexes, a hash uses any object as the key.
Creating Hashes
To create a hash within Ruby, we can simply use the {} braces, surrounding a set of elements. We can also initialize an empty hash using the empty {} braces:
2.1.1 :001 > score = { "Joe Bloggs" => 10, "Sarah Bloggs" => 8 } => {"Joe Bloggs"=>10, "Sarah Bloggs"=>8}2.1.1 :002 > stock = {} => {}
When creating a hash, an alternative syntax is available using symbols as the index, this is shown by using the :key_name syntax, which ...
Get Ruby Quick Syntax Reference now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.