O'Reilly logo

Ruby Quick Syntax Reference by Matt Clements

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

CHAPTER 5

image

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required