Created
January 3, 2009 00:22
-
-
Save mischa/42748 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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