Sha256: eab0accf5a20a90e3ac323d85497d7c97b5b58854c46fb3796e8ad793b564e6d

Contents?: true

Size: 1.36 KB

Versions: 10

Compression:

Stored size: 1.36 KB

Contents

# = reference.rb
#
# == Copyright (c) 2005 Thomas Sawyer
#
#   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.
#
# == Author(s)
#
# * Thomas Sawyer

#require 'facets/core/kernel/as'
#require 'facets/core/kernel/object_class'

# = 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 :self, __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

10 entries across 10 versions & 1 rubygems

Version Path
facets-1.7.0 lib/facets/more/reference.rb
facets-1.7.30 lib/facets/more/reference.rb
facets-1.7.38 lib/facets/more/reference.rb
facets-1.7.46 lib/facets/more/reference.rb
facets-1.8.20 lib/facets/more/reference.rb
facets-1.8.0 lib/facets/more/reference.rb
facets-1.8.49 lib/facets/more/reference.rb
facets-1.8.51 lib/facets/more/reference.rb
facets-1.8.54 lib/facets/more/reference.rb
facets-1.8.8 lib/facets/more/reference.rb