Skip to content

Instantly share code, notes, and snippets.

@mischa
Created January 3, 2009 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mischa/42748 to your computer and use it in GitHub Desktop.
Save mischa/42748 to your computer and use it in GitHub Desktop.
# Inject
%w{earl-grey peach white}.inject({}) do |hash, string|
hash[string] = "tea"
hash
end
=> {"white"=>"tea", "earl-grey"=>"tea", "peach"=>"tea"}
# Each with object
>> %w{earl-grey peach white}.each_with_object({}){|string, hash| hash[string] = "tea"}
=> {"white"=>"tea", "earl-grey"=>"tea", "peach"=>"tea"}
# Inject: fails unless we return the accumulator.
>> %w{earl-grey peach white}.inject({}){|hash, string| hash[string] = "tea"}
IndexError: string not matched
from (irb):4:in `[]='
from (irb):4
from (irb):4:in `inject'
from (irb):4:in `each'
from (irb):4:in `inject'
from (irb):4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment