Download ruby eBook


A Hash is a dictionary-like collection of unique keys and their values. Also called associative arrays, they are similar to Arrays, but where an Array uses integers as its index, a Hash allows you to use any object type. You retrieve or create a new entry in a Hash by referring to its key.


  • { first_name: "Noel", second_name: "Edmonds" }

  • { :first_name => "Noel", :second_name => "Edmonds" }

  • { "First Name" => "Noel", "Second Name" => "Edmonds" }

  • { first_key => first_value, second_key => second_value }


Hashes in Ruby map keys to values using a hash table.

Any hashable object can be used as keys. However, it's very common to use a Symbol as it is generally more efficient in several Ruby versions, due to the reduced object allocation.

{ key1: "foo", key2: "baz"  }

Related Examples


1378 Contributors: 67
Thursday, July 20, 2017
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook