lib/sugar-high/hash.rb in sugar-high-0.6.0 vs lib/sugar-high/hash.rb in sugar-high-0.6.1
- old
+ new
@@ -1,7 +1,16 @@
require 'sugar-high/arguments'
class Hash
+
+ # http://www.dweebd.com/ruby/hash-key-rewrite/
+ def rewrite mapping
+ inject({}) do |rewritten_hash, (original_key, value)|
+ rewritten_hash[mapping.fetch(original_key, original_key)] = value
+ rewritten_hash
+ end
+ end
+
def hash_revert
r = Hash.new {|h,k| h[k] = []}
each {|k,v| r[v] << k}
r
end