Sha256: d973a7dcaba66d222cfa1b66e2be00cce08d9c83893f140584cf6c3ef451a053

Contents?: true

Size: 1008 Bytes

Versions: 10

Compression:

Stored size: 1008 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
  #
  # TODO: Rename to #aliaskey or something else.

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

end

Version data entries

10 entries across 9 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/hash/alias.rb
facets-3.1.0 lib/core/facets/hash/alias.rb
facets-3.0.0 lib/core/facets/hash/alias.rb
facets-2.9.3 lib/core/facets/hash/alias.rb
facets-2.9.2 src/core/facets/hash/alias.rb
facets-2.9.2 lib/core/facets/hash/alias.rb
facets-2.9.1 lib/core/facets/hash/alias.rb
facets-2.9.0 lib/core/facets/hash/alias.rb
facets-2.9.0.pre.2 lib/core/facets/hash/alias.rb
facets-2.9.0.pre.1 lib/core/facets/hash/alias.rb