1. Computing

Hash Reference

By

Hash Reference

Below are a list of the most useful Hash methods. For a full list, see the official Hash documentation. Hash is a very widely used class and has a lot of methods. However, most of them are rarely used, only useful in a few edge cases. Below is the list of most useful Hash methods as of Ruby 1.9.3.

  • The index operator [] - By far the most common usage of a Hash. While there is the simple usage, passing a single hash key and getting a single value like some_hash[:key], there's slightly more you can do with it. If the Hash does not have a value associated with that key, it will check its default and default_proc attributes (see below), possibly running any code associated with default_proc. This can be used to emulate missing keys, raise errors, etc.
  • The equality operator == - The equality operator acts more or less as you'd expect. As an optimization, Ruby will first check of both hashes have the same number of keys. If they do, it will compare each key with each value using the object's associated equality operator and if all return true, the hashes are equal. Be aware that this can be a very expensive operation.
  • Hash#clear - This will delete all key/value pairs, but leave the Hash object itself intact. Why not just assign a new empty Hash object to the variable? What if the Hash is referenced in other places? It may be impossible or difficult to track down all these reference, clearing it once clears it everwhere.
  • The default attribute - If the index operator tries to access a key where nothing has been stored yet, the object stored in the default attribute will be returned.
  • The default_proc attribute - Just like with default attribute, the default_proc attribute is used when the index operator tries to access a key where nothing has been stored yet. But with default_proc, the proc object held there will be run with two arguments: the hash itself and the key it tried to access.
  • Hash#delete(key) and Hash#delete_if{|hash,key| … } - This deletes the key/value pair associated with the key. This is the only way to delete a key. This is not the same as hash[:key] = nil, that doesn't actually delete the key and doesn't take the Hash's default attribute into account. Similarly, delete_if will iterate over all the keys and if the block passed returns true, delete that key.
  • Hash#each{|k,v| … } and Hash#each_pair{|k,v| … } - These are the basic ways to iterate over a hash. While Hashes are not built to iterate over (it's much slower to iterate over a hash than it is to iterate over an Array), there still may be times when you need to do this. Both of these methods do the same thing, and the block takes two arguments: the key and value. The most obvious use of this method is to print out a hash for debugging or reporting purposes.
  • Hash#keys and Hash#has_key?(key) - The keys method will return an array of the keys in the hash. This is very useful, especially if the hash is used for data collection. Similarly, the has_key? method will return true if the hash has the key passed in the method call. This is functionally equivalent to hash.keys.include?(key), but faster.
  • Hash#invert - This is a bit of an odd duck, but useful in some places. This will make a new hash and for every key/value pair in the hash, reverse the key/value pair. In essence, make a copy of the hash but the values are the keys and the keys are the values. So { :a => 10 }.invert will yield { 10 => :a }. Seems useless at first, and it's not very useful for many uses of hashes, but it can be useful after data collection. However, be aware that since hashes are unordered and values do not need to be unique, if the same value occurrs twice there's no defined behavior. For example, if { :a => 10, :b => 10 } is inverted, there's no way of telling whether { 10 => :a } or { 10 => :b } will emerge.
  • Hash#merge and Hash#merge! - Merge the hashes together. Calling h1.merge(h2) will merge all of the keys from h2 into h1, overwriting any keys that exist in both, and returns a new hash. The destructive cousin merge! merges directly in h1.
  • Hash#length and Hash#size - These are pretty self-explanatory. These are equivalent to calling hash.keys.length, returns the number of keys in the hash.
  1. About.com
  2. Computing
  3. Ruby
  4. Beginning Ruby
  5. Ruby's Basic Features
  6. Hash Reference

©2014 About.com. All rights reserved.