Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/library/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#rekeyObject

Transform keys of hash returning a new hash.



18
19
20
21
22
23
24
# File 'lib/library/core_ext.rb', line 18

def rekey #:yield:
  if block_given?
    inject({}){|h,(k,v)| h[yield(k)]=v; h}
  else
    inject({}){|h,(k,v)| h[k.to_sym]=v; h}
  end
end

#rekey!Object

In-place rekey.



29
30
31
# File 'lib/library/core_ext.rb', line 29

def rekey! #:yield:
  replace(rekey{|k| yield(k) })
end