Class: ReachingArray

Inherits:
Object
  • Object
show all
Defined in:
lib/reachable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(retract) ⇒ ReachingArray

Returns a new instance of ReachingArray.



33
34
35
# File 'lib/reachable.rb', line 33

def initialize(retract)
  @retract = retract
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

The method could be missing from the array, the members, or both. Try to pass the method to each, in that order, accepting the first taker



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/reachable.rb', line 39

def method_missing(method, *args, &block)
  if @retract.respond_to?(method)
    # If this is an array method, just use that
    r = @retract.send(method, *args, &block)
    return r
  else
    # If this is an object-specific method, run an each on it.
    # A simple each won't work because that doesn't modify the
    # array elements in place as we want, so have to use collect
    # instead.
    @retract = @retract.collect do |o|
      unless o.kind_of?(Array)
        o.send(method, *args, &block)
      else
        # Update in 0.2.1: If the element of the array is an array
        # itself, then operate on it as a ReachingArray as well.
        o.reach.send(method, *args, &block).retract
      end
    end
    return self
  end
end

Instance Attribute Details

#retractObject

The array that this reaching array is extending. This might be a real Array, or a ReachingArray



31
32
33
# File 'lib/reachable.rb', line 31

def retract
  @retract
end

Instance Method Details

#==(another) ⇒ Object

Equality test - equality of the underlying retract arrays is all that matter



64
65
66
# File 'lib/reachable.rb', line 64

def ==(another)
  @retract <=> another.retract
end

#slapObject



72
73
74
# File 'lib/reachable.rb', line 72

def slap
  retract.slap
end

#to_sObject



68
69
70
# File 'lib/reachable.rb', line 68

def to_s
  method_missing(:to_s)
end