Sha256: 50017b96578ebb04b08f0018d09695de9dee1fdcc3d93201722d3cf66af9c902

Contents?: true

Size: 1.47 KB

Versions: 13

Compression:

Stored size: 1.47 KB

Contents

# TITLE:
#
#   Reference
#
# SUMMARY:
#
#   Reference provides a way to access object indirectly.
#   This allows for the object itself to be changed on the fly.
#
# COPYRIGHT:
#
#   Copyright (c) 2005 Thomas Sawyer
#
# LICENSE:
#
#   Ruby License
#
#   This module is free software. You may use, modify, and/or redistribute this
#   software under the same terms as Ruby.
#
#   This program is distributed in the hope that it will be useful, but WITHOUT
#   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#   FOR A PARTICULAR PURPOSE.
#
# AUTHORS:
#
#   - Thomas Sawyer


# = Reference
#
# Reference provides a way to access object indirectly.
# This allows for the object itself to be changed on the fly.
#
# == Synopsis
#
#   a = "HELLO"
#   b = ref(a)
#   puts b    #=> "HELLO"
#   c = 10
#   b.become(c)
#   puts b    #=> "10"
#

class Reference

  # Privatize most Kernel methods.

  private *instance_methods

  def self.new(obj)
    ref = allocate
    ref.become obj
    ref
  end

  def method_missing(*args, &block)
    @ref.__send__(*args, &block)
  end

  def become(obj)
    old = @ref
    @ref = obj
    old
  end

  def __value__
    @ref
  end

  alias_method :instance_delegate, :__value__
end


module Kernel

  # Shortcut reference constructor.

  def ref(x)
    Reference.new(x)
  end

end



#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#

=begin #test

  require 'test/unit'

  # TODO

=end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
facets-2.0.1 lib/more/facets/reference.rb
facets-2.0.0 lib/more/facets/reference.rb
facets-2.0.2 lib/more/facets/reference.rb
facets-2.0.5 lib/more/facets/reference.rb
facets-2.1.0 lib/more/facets/reference.rb
facets-2.1.2 lib/more/facets/reference.rb
facets-2.1.1 lib/more/facets/reference.rb
facets-2.0.3 lib/more/facets/reference.rb
facets-2.0.4 lib/more/facets/reference.rb
facets-2.1.3 lib/more/facets/reference.rb
facets-2.2.0 lib/more/facets/reference.rb
facets-2.2.1 lib/more/facets/reference.rb
facets-2.3.0 lib/class/facets/reference.rb