aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/hash/keys.rb
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2012-05-23 07:27:21 -0700
committerRafael Mendonça França <rafaelmfranca@gmail.com>2012-05-23 07:27:21 -0700
commitc1487f619045840d89f1e2dbc6e133e1c2cbf2f4 (patch)
treed27cdae39be7aa69ecaf9e918ecff176a51be268 /activesupport/lib/active_support/core_ext/hash/keys.rb
parenta6d0e3795790c85dded4f2c396277214508dbc89 (diff)
parent6ff887321b930197a8d62ddd0fc1dcb965ccda29 (diff)
downloadrails-c1487f619045840d89f1e2dbc6e133e1c2cbf2f4.tar.gz
rails-c1487f619045840d89f1e2dbc6e133e1c2cbf2f4.tar.bz2
rails-c1487f619045840d89f1e2dbc6e133e1c2cbf2f4.zip
Merge pull request #6259 from markmcspadden/hash_transform_keys
Add Hash#transform_keys and Hash#transform_keys!
Diffstat (limited to 'activesupport/lib/active_support/core_ext/hash/keys.rb')
-rw-r--r--activesupport/lib/active_support/core_ext/hash/keys.rb43
1 files changed, 25 insertions, 18 deletions
diff --git a/activesupport/lib/active_support/core_ext/hash/keys.rb b/activesupport/lib/active_support/core_ext/hash/keys.rb
index be4d611ce7..8b24ef3350 100644
--- a/activesupport/lib/active_support/core_ext/hash/keys.rb
+++ b/activesupport/lib/active_support/core_ext/hash/keys.rb
@@ -1,46 +1,53 @@
class Hash
- # Return a new hash with all keys converted to strings.
+ # Return a new hash with all keys converted using the block operation.
#
- # { :name => 'Rob', :years => '28' }.stringify_keys
- # #=> { "name" => "Rob", "years" => "28" }
- def stringify_keys
+ # { :name => 'Rob', :years => '28' }.transform_keys{ |key| key.to_s.upcase }
+ # # => { "NAME" => "Rob", "YEARS" => "28" }
+ def transform_keys
result = {}
keys.each do |key|
- result[key.to_s] = self[key]
+ result[yield(key)] = self[key]
end
result
end
- # Destructively convert all keys to strings. Same as
- # +stringify_keys+, but modifies +self+.
- def stringify_keys!
+ # Destructively convert all keys using the block operations.
+ # Same as transform_keys but modifies +self+
+ def transform_keys!
keys.each do |key|
- self[key.to_s] = delete(key)
+ self[yield(key)] = delete(key)
end
self
end
+ # Return a new hash with all keys converted to strings.
+ #
+ # { :name => 'Rob', :years => '28' }.stringify_keys
+ # #=> { "name" => "Rob", "years" => "28" }
+ def stringify_keys
+ transform_keys{ |key| key.to_s }
+ end
+
+ # Destructively convert all keys to strings. Same as
+ # +stringify_keys+, but modifies +self+.
+ def stringify_keys!
+ transform_keys!{ |key| key.to_s }
+ end
+
# Return a new hash with all keys converted to symbols, as long as
# they respond to +to_sym+.
#
# { 'name' => 'Rob', 'years' => '28' }.symbolize_keys
# #=> { :name => "Rob", :years => "28" }
def symbolize_keys
- result = {}
- keys.each do |key|
- result[(key.to_sym rescue key)] = self[key]
- end
- result
+ transform_keys{ |key| key.to_sym rescue key }
end
alias_method :to_options, :symbolize_keys
# Destructively convert all keys to symbols, as long as they respond
# to +to_sym+. Same as +symbolize_keys+, but modifies +self+.
def symbolize_keys!
- keys.each do |key|
- self[(key.to_sym rescue key)] = delete(key)
- end
- self
+ transform_keys!{ |key| key.to_sym rescue key }
end
alias_method :to_options!, :symbolize_keys!