Class: Reference

Inherits:
Object show all
Defined in:
lib/supplemental/facets/reference.rb

Overview

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)
b.to_s    #=> "HELLO"
c = 10
b.become(c)
b.to_s    #=> "10"

TODO: Use BasicObject for Ruby 1.9.

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



57
58
59
# File 'lib/supplemental/facets/reference.rb', line 57

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

Class Method Details

.new(obj) ⇒ Object



51
52
53
54
55
# File 'lib/supplemental/facets/reference.rb', line 51

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

Instance Method Details

#__value__Object Also known as: instance_delegate



67
68
69
# File 'lib/supplemental/facets/reference.rb', line 67

def __value__
  @ref
end

#become(obj) ⇒ Object



61
62
63
64
65
# File 'lib/supplemental/facets/reference.rb', line 61

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