Sha256: 9b60bfdb300df3c9dece24208f3700a61ab2cc60ea5af0daf4c15c9b36472de7

Contents?: true

Size: 951 Bytes

Versions: 4

Compression:

Stored size: 951 Bytes

Contents

#--
# Credit goes to Gavin Sinclair.
#++
class Hash
  # 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.
  #
  #   require 'facet/hash/alias!'
  #
  #   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', 'name'=>'Gavin', 'wife'=>:Lisa, 'spouse'=>:Lisa }
  #
  # Note that if the _oldkey_ is reassigned, the reference will no longer exist,
  # and the _newkey_ will remain as it was.
  def alias!( newkey, oldkey )
    self[newkey] = self[oldkey] if self.has_key?(oldkey)
    self
  end
end 

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
facets-0.6.3 lib/facet/hash/alias!.rb
facets-0.7.0 lib/facet/hash/alias%21.rb
facets-0.7.1 lib/facet/hash/alias%21.rb
facets-0.7.2 lib/facet/hash/alias%21.rb