Tutorial by Examples

A hash in Ruby is an object that implements a hash table, mapping keys to values. Ruby supports a specific literal syntax for defining hashes using {}: my_hash = {} # an empty hash grades = { 'Mark' => 15, 'Jimmy' => 10, 'Jack' => 10 } A hash can also be created using the standard new...
Individual values of a hash are read and written using the [] and []= methods: my_hash = { length: 4, width: 5 } my_hash[:length] #=> => 4 my_hash[:height] = 9 my_hash #=> {:length => 4, :width => 5, :height => 9 } By default, accessing a key which has not been added t...
By default, attempting to lookup the value for a key which does not exist will return nil. You can optionally specify some other value to return (or an action to take) when the hash is accessed with a non-existent key. Although this is referred to as "the default value", it need not be a s...
Hash has a default value for keys that are requested but don't exist (nil): a = {} p a[ :b ] # => nil When creating a new Hash, one can specify the default: b = Hash.new 'puppy' p b[ :b ] # => 'puppy' Hash.new also takes a block, which allows you to automatically create n...
You can create a new hash with the keys or values modified, indeed you can also add or delete keys, using inject (AKA, reduce). For example to produce a hash with stringified keys and upper case values: fruit = { name: 'apple', color: 'green', shape: 'round' } # => {:name=>"apple",...
A Hash includes the Enumerable module, which provides several iteration methods, such as: Enumerable#each, Enumerable#each_pair, Enumerable#each_key, and Enumerable#each_value. .each and .each_pair iterate over each key-value pair: h = { "first_name" => "John", "last_na...
Hashes can be freely converted to and from arrays. Converting a hash of key/value pairs into an array will produce an array containing nested arrays for pair: { :a => 1, :b => 2 }.to_a # => [[:a, 1], [:b, 2]] In the opposite direction a Hash can be created from an array of the same form...
{foo: 'bar', biz: 'baz'}.keys # => [:foo, :biz] {foo: 'bar', biz: 'baz'}.values # => ["bar", "baz"] {foo: 'bar', biz: 'baz'}.to_a # => [[:foo, "bar"], [:biz, "baz"]] {foo: 'bar', biz: 'baz'}.each #<Enumerator: {:foo=>"bar", :b...
Ruby hashes use the methods hash and eql? to perform the hash operation and assign objects stored in the hash to internal hash bins. The default implementation of hash in Ruby is the murmur hash function over all member fields of the hashed object. To override this behavior it is possible to overrid...
select returns a new hash with key-value pairs for which the block evaluates to true. { :a => 1, :b => 2, :c => 3 }.select { |k, v| k != :a && v.even? } # => { :b => 2 } When you will not need the key or value in a filter block, the convention is to use an _ in that place:...
Intersection of Hashes To get the intersection of two hashes, return the shared keys the values of which are equal: hash1 = { :a => 1, :b => 2 } hash2 = { :b => 2, :c => 3 } hash1.select { |k, v| (hash2.include?(k) && hash2[k] == v) } # => { :b => 2 } Union (...

Page 1 of 1