diff options
author | Attila Domokos <adomokos@gmail.com> | 2014-08-21 23:07:03 -0500 |
---|---|---|
committer | Attila Domokos <adomokos@gmail.com> | 2014-08-21 23:13:37 -0500 |
commit | 36895bd90fe02dbbf2c8461dffb30eb25c7cf3e1 (patch) | |
tree | 8e61a0c6359656afc5b73fbb360a857613e88188 /activemodel | |
parent | a57f7b57976c74dd76c911f867b76197119546e6 (diff) | |
download | rails-36895bd90fe02dbbf2c8461dffb30eb25c7cf3e1.tar.gz rails-36895bd90fe02dbbf2c8461dffb30eb25c7cf3e1.tar.bz2 rails-36895bd90fe02dbbf2c8461dffb30eb25c7cf3e1.zip |
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.
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/lib/active_model/errors.rb | 6 |
1 files changed, 2 insertions, 4 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index fc8034f9c7..f7126c1eb8 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -251,11 +251,9 @@ module ActiveModel # 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 |