Learn Ruby: The Beginner Guide: An Introduction to Ruby Programming

Learn Ruby: The Beginner Guide: An Introduction to Ruby Programming

Author:Jake Day Williams [Williams, Jake Day]
Language: eng
Format: epub
Published: 2014-12-15T18:30:00+00:00


puts :im_a_symbol.object_id

puts :im_a_symbol.object_id

Notice that both instances of the strings have different ID numbers, while the symbols have the same ID number. We will talk more about symbols soon.

Hashes

Hashes are similar to arrays. A key difference is that arrays use integers (0, 1, 2,...) for indexing, while a hash can use other object types.

The implicit form is written like this:

age = {"Gilbert" => 25, "Tammy" => 32}

The hash name is age. The hash holds two keys and two associated values. Gilbert is the key for the value 25. Notice that we didn't use a simple equals symbol to set our keys equal to our values. We used what's called a hash rocket. Hash rockets are easy to remember because they look like a rocket. Look at it: => It's beautiful.

We access the values the same way we would with an array. The difference is we use the key name instead of an index number.

puts age["Gilbert"]

25



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.