lib/core/facets/hash/rekey.rb in facets-2.4.5 vs lib/core/facets/hash/rekey.rb in facets-2.5.0

- old
+ new

@@ -1,14 +1,20 @@ require 'facets/symbol/to_proc' class Hash - # rekey(to_key, from_key) - # rekey{ |key| ... } + # Rekey a hash. # + # rekey() + # rekey(to_key, from_key) + # rekey{ |key| ... } + # + # If no arguments or block are given, then all keys are converted + # to Symbols. + # # If two keys are given, then the second key is changed to - # the first. + # the first. You can think of it as +alias+ for hash keys. # # foo = { :a=>1, :b=>2 } # foo.rekey('a',:a) #=> { 'a'=>1, :b=>2 } # foo.rekey('b',:b) #=> { 'a'=>1, 'b'=>2 } # foo.rekey('foo','bar') #=> { 'a'=>1, 'b'=>2 } @@ -19,12 +25,11 @@ # # foo = { :name=>'Gavin', :wife=>:Lisa } # foo.rekey{ |k| k.to_s } #=> { "name"=>"Gavin", "wife"=>:Lisa } # foo.inspect #=> { :name =>"Gavin", :wife=>:Lisa } # - # CREDIT: Trans - # CREDIT: Gavin Kistner + # CREDIT: Trans, Gavin Kistner def rekey(*args, &block) dup.rekey!(*args, &block) end @@ -32,11 +37,10 @@ # # foo = { :name=>'Gavin', :wife=>:Lisa } # foo.rekey!{ |k| k.to_s } #=> { "name"=>"Gavin", "wife"=>:Lisa } # foo.inspect #=> { "name"=>"Gavin", "wife"=>:Lisa } # - # CREDIT: Trans - # CREDIT: Gavin Kistner + # CREDIT: Trans, Gavin Kistner def rekey!(*args, &block) # for backward comptability (TODO: DEPRECATE). block = args.pop.to_sym.to_proc if args.size == 1 # if no args use block.