1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Hash Quick Reference

By

Hash Quick Reference

Hashes are one of the most used classes in Ruby. However, with each release the list of methods just keeps growing and growing. This article cuts through all the noise and just tells you very succinctly how to do the things you need to do with Hashes.

Creating Hashes

  • The hash literal is the most common and often most useful want to create hashes. Hash literals are enclosed in curly braces and are a list of key/value pairs. The hash literal {:a => 10, :b => 20 } will produce a hash with two keys, the symbols a and b, with the values 10 and 20 respectively. I all your keys are symbols, you can use a slightly more succinct syntax, {a:10, b:20}.
  • Empty hashes can be created with either the empty hash literal {} or the Hash.new constructor with no arguments.
  • Hashes can be created from an array of key/value pairs with the Hash[] method. If you have the array a = [:a, 10, :b, 20], you can create the hash {a:10, b:20} from the array by calling Hash[*a]. Note the splat operator on the array.

Indexing Hashes

Once you have a hash, you usually need to index it in some way. Indexing a hash allows you to store and retrieve values referenced by a key. The keys in this example are all symbols, as this is idiomatic Ruby, but the keys can be any object.

  • Retrieving values is done with the index operator. If you have the hash h = {a:10, b:20}, then h[:a] will return 10.
  • Storing values is done with the index assignment operator. If you have the hash h = {a:10, b:20}, then h[:a] = 30 will change the hash to {a:30, b:20}.
  • By default, if a hash does not have an indexed element, it will return nil. If you have h = {a:10, b:20} and call h[:c], it will return nil. If you want it to return something different, you can set the default object with h.default = "MISSING", and now h[:c] will return the string "MISSING."
  • If you just want to know if the hash has a value for a specific key, but don't really care about the value itself, you can use the has_key? method. If you have the hash h = {a:10, b:20}, then h.has_key?(:a) will return true, while h.has_key?(:monkeys) will return false.

Get All Keys or Values

  • Getting a list of keys from a hash is as simple as calling the keys method. If you have the hash h = {a:10, b:20}, then calling h.keys will return the array [:a, :b].
  • Getting a list of values from a hash can similarly be done by calling the values method. If you have the hash h = {a:10, b:20}, then calling h.values will return the array [10, 20].
  • Getting a list of all keys and values can be done by calling the to_a method (to array, you're essentially converting the entire hash to an array). If you have the hash h = {a:10, b:20}, then calling h.to_a will return the array [[:a, 10], [:b, 20]].

Merging Hashes

You'll often have two hashes and need to merge them into one. For example, a hash of default values and a hash of user-defined values.

  • If you have the hash defaults = {a:10} and input = {b:20}, then defaults.merge(input) will return the hash {a:10, b:20}. However, if you had defaults = {a:10} and input = {a:20} (note, they both use the :a key), then defaults.merge(input) will return the hash {a:20}.
  • The merge method returns a new hash, and leaves the values alone in both the original hashes. If you simply want to merge all the values from one hash into another without creating a third hash, you can use the merge! method. If you have the hash defaults = {a:10} and input = {b:20}, then defaults.merge!(input) will both return the hash {a:10, b:20} and the defaults hash will equal {a:10, b:20}.
  1. About.com
  2. Technology
  3. Ruby
  4. Beginning Ruby
  5. Hash Quick Reference

©2014 About.com. All rights reserved.