Sha256: 559f5d00a551181e91f218b7c8eef4084bb2c102a436ac1e935c468790b893bc

Contents?: true

Size: 1.52 KB

Versions: 17

Compression:

Stored size: 1.52 KB

Contents

# = 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"
#
# == Authors
#
# * Thomas Sawyer
#
# == Copying
#
# 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.

# = Reference
#
# Reference provides a way to access object indirectly.
# This allows for the object itself to be changed on the fly.
#
#   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

17 entries across 17 versions & 2 rubygems

Version Path
facets-2.8.4 lib/more/facets/reference.rb
facets-2.8.3 lib/more/facets/reference.rb
facets-2.8.2 lib/more/facets/reference.rb
facets-2.8.1 lib/more/facets/reference.rb
facets-2.8.0 lib/more/facets/reference.rb
facets-2.7.0 lib/more/facets/reference.rb
facets-2.6.0 lib/more/facets/reference.rb
facets-2.4.0 lib/facets/reference.rb
facets-2.4.1 lib/facets/reference.rb
facets-2.4.4 lib/more/facets/reference.rb
facets-2.4.3 lib/more/facets/reference.rb
facets-2.4.2 lib/more/facets/reference.rb
facets-2.5.1 lib/more/facets/reference.rb
facets-2.4.5 lib/more/facets/reference.rb
facets-2.5.0 lib/more/facets/reference.rb
facets-2.5.2 lib/more/facets/reference.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/more/facets/reference.rb