Permalink
Browse files

Using `each_with_object` instead of `reduce`

This way no new object allocation is taking place. Thanks @jeremy for
the suggestion!
  • Loading branch information...
1 parent 36895bd commit 1079bb6d7dafa975fbe6b56fbf8824c84e7fca17 @adomokos adomokos committed Aug 22, 2014
Showing with 2 additions and 2 deletions.
  1. +2 −2 activemodel/lib/active_model/errors.rb
@@ -251,8 +251,8 @@ def as_json(options=nil)
# person.errors.to_hash(true) # => {:name=>["name cannot be nil"]}
def to_hash(full_messages = false)
if full_messages
- self.messages.reduce({}) do |messages, (attribute, array)|
- messages.merge!(attribute => array.map { |message| full_message(attribute, message) })
+ self.messages.each_with_object({}) do |(attribute, array), messages|
+ messages[attribute] = array.map { |message| full_message(attribute, message) }
end
else
self.messages.dup

0 comments on commit 1079bb6

Please sign in to comment.