Sha256: 28d7ae5aafc7f3e5ae5ae2795997670310e69e7397c6aafbdf5a4dd63243257c

Contents?: true

Size: 955 Bytes

Versions: 2

Compression:

Stored size: 955 Bytes

Contents

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.
  #
  #   foo = { :name=>'Gavin', 'wife'=>:Lisa }
  #   foo.alias!('name',:name)     => { :name=>'Gavin', 'name'=>'Gavin', 'wife'=>:Lisa }
  #
  #   foo = { :name=>'Gavin', 'wife'=>:Lisa }
  #   foo.alias!('spouse','wife')  => { :name=>'Gavin', 'wife'=>:Lisa, 'spouse'=>:Lisa }
  #
  #   foo = { :name=>'Gavin', 'wife'=>:Lisa }
  #   foo.alias!('bar','foo')      => { :name=>'Gavin', 'wife'=>:Lisa }
  #
  # Note that if the _oldkey_ is reassigned, the reference will no longer exist,
  # and the _newkey_ will remain as it was.
  #
  #   CREDIT: Gavin Sinclair

  def alias!( newkey, oldkey )
    self[newkey] = self[oldkey] if self.has_key?(oldkey)
    self
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
facets-2.3.0 lib/core/facets/hash/alias.rb
facets-2.2.1 lib/core/facets/hash/alias.rb