December 2014
Intermediate to advanced
164 pages
2h 14m
English
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 ...
Read now
Unlock full access