 
 This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.
| Building Up a Hash Using Injection | ||
|---|---|---|
| Code | Expected | Actual | 
| collection = [ [1, 'one'], [2, 'two'], [3, 'three'], 
               [4, 'four'], [5, 'five']
             ]
collection.inject({}) do |hash, value|
  hash[value.first] = value.last
  hash
end | {5=>"five", 1=>"one", 2=>"two", 3=>"three", 4=>"four"} | {5=>"five", 1=>"one", 2=>"two", 3=>"three", 4=>"four"} | 
| collection.dup.inject({}) { |hash, value| hash[value.first] = value.last } | IndexError: index 3 out of string ... | IndexError: index 3 out of string from (irb):8:in `[]=' from (irb):8 from (irb):8:in `inject' from (irb):8 | 
| Hash.new["key"] = "some value" | "some value" | "some value" | 
| collection.inject({}) do |hash, value|
  hash.update value.first => value.last
end | {5=>"five", 1=>"ontwo", 2=>"two", 3=>"three", 4=>"four"} | {5=>"five", 1=>"ontwo", 2=>"two", 3=>"three", 4=>"four"} | 
| class Array
  def to_h(default=nil)
    Hash[ *inject([]) { |a, value| a.push value, default || yield(value) } ]
  end
end
a = [1, 2, 3]
a.to_h(true) | {1=>true, 2=>true, 3=>true} | {1=>true, 2=>true, 3=>true} | 
| a.to_h { |value| [value * -1, value * 2] } | {1=>[-1, 2], 2=>[-2, 4], 3=>[-3, 6]} | {1=>[-1, 2], 2=>[-2, 4], 3=>[-3, 6]} |