I ran into a case where I needed to merge an array of hashes and was
disappointed that I couldn''t use ActiveSupport''s
Enumerable#sum
extension solely for want of Hash#+ ... so I added it as an alias to
Hash#merge for fun.
Given:
a = {:a => 1, :b => 2}
b = {:b => 3, :c => 4}
c = {:a => 2, :d => 5}
d = [a, b, c]
... what was previously written:
a.merge(b)
d.inject {|sum, h| sum.merge(h)}
... can now be written:
a + b
d.sum #=> {:a => 2, :b => 3, :c => 4, :d => 5}
My favorite usage, though, reads something like:
def create_foo_with_defaults(options = {})
defaults = {...}
Foo.create!(defaults + options)
end
Ticket and patch (vs. r8062, with tests):
http://dev.rubyonrails.org/ticket/10047
Please review and comment/+1.
Thanks!
--j
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Core" group.
To post to this group, send email to rubyonrails-core@googlegroups.com
To unsubscribe from this group, send email to
rubyonrails-core-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---