Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Replacing an each with reduce

The functionality has not changed, but the code is more elegant by
using `reduce` instead of `each`.

This way no accumulator needs to be declared, no explicit return is
needed.
  • Loading branch information...
commit 36895bd90fe02dbbf2c8461dffb30eb25c7cf3e1 1 parent a57f7b5
@adomokos adomokos authored
Showing with 2 additions and 4 deletions.
  1. +2 −4 activemodel/lib/active_model/errors.rb
View
6 activemodel/lib/active_model/errors.rb
@@ -251,11 +251,9 @@ def as_json(options=nil)
# person.errors.to_hash(true) # => {:name=>["name cannot be nil"]}
def to_hash(full_messages = false)
if full_messages
- messages = {}
- self.messages.each do |attribute, array|
- messages[attribute] = array.map { |message| full_message(attribute, message) }
+ self.messages.reduce({}) do |messages, (attribute, array)|
+ messages.merge!(attribute => array.map { |message| full_message(attribute, message) })
end
- messages
else
self.messages.dup
end
Please sign in to comment.
Something went wrong with that request. Please try again.