Hash (Class)

In: basiclibrary.rb
Parent: Object

Extensions to the Hash class

Public Instance methods

Modifies the receiving Hash so that the value previously referred to by oldkey is also referenced by newkey; oldkey is retained in the Hash. If oldkey does not exist as a key in the Hash, no change is effected.

Returns a reference to the Hash.

  foo = { :name=>'Gavin', 'wife'=>:Lisa }
  foo.alias!('name',:name)     => { :name=>'Gavin', 'name'=>'Gavin', 'wife'=>:Lisa }
  foo.alias!('spouse','wife')  => { :name=>'Gavin', 'name'=>'Gavin', 'wife'=>:Lisa, 'spouse'=>:Lisa }
  foo.alias!('bar','foo')      => { 'name'=>'Gavin', 'spouse'=>:Lisa }

Converts all Symbol keys in the Hash to be String values, returning a new Hash.

  foo = { :name=>'Gavin', 'wife'=>:Lisa }
  foo.convert_symbol_keys  #=>  { "name"=>"Gavin", "wife"=>:Lisa }
  foo.inspect              #=>  { :name =>"Gavin", "wife"=>:Lisa }

Synonym for Hash#convert_symbol_keys, but modifies the receiver (and returns it).

  foo = { :name=>'Gavin', 'wife'=>:Lisa }
  foo.convert_symbol_keys!  #=>  { "name"=>"Gavin", "wife"=>:Lisa }
  foo.inspect               #=>  { "name"=>"Gavin", "wife"=>:Lisa }

Modifies the receiving Hash so that the value previously referred to by oldkey is referenced by newkey; oldkey is removed from the Hash. If oldkey does not exist as a key in the Hash, no change is effected.

Returns a reference to the Hash.

  foo = { :name=>'Gavin', 'wife'=>:Lisa }
  foo.swapkey!('name',:name)     #=> { 'name'=>'Gavin', 'wife'=>:Lisa }
  foo.swapkey!('spouse','wife')  #=> { 'name'=>'Gavin', 'spouse'=>:Lisa }
  foo.swapkey!('bar','foo')      #=> { 'name'=>'Gavin', 'spouse'=>:Lisa }

Converts all keys in the Hash to be Symbol values where possible, returning a new Hash.

  foo = { :name=>'Gavin', 'wife'=>:Lisa }
  foo.symbolify_keys  #=>  { :name=>"Gavin", :wife =>:Lisa }
  foo.inspect         #=>  { :name=>"Gavin", "wife"=>:Lisa }

Synonym for Hash#symbolify_keys, but modifies the receiver (and returns it).

  foo = { :name=>'Gavin', 'wife'=>:Lisa }
  foo.symbolify_keys!  #=>  { :name=>"Gavin", :wife=>:Lisa }
  foo.inspect          #=>  { :name=>"Gavin", :wife=>:Lisa }

[Validate]