[Note: parts of this message were removed to make it a legal post.]
By the way, I'm sure it's not infinite recursion. It's just the nature of
my algorithm:
class PlaybackBase
def record(&block)
return PlaybackChain.new(self, block)
end
end
class PlaybackNil < PlaybackBase
def initialize(root)
@root = root
end
def play_back(&block)
block.call(@root)
end
end
class PlaybackChain < PlaybackBase
def initialize(tail, block)
@tail = tail
@block = block
end
def play_back(&block)
@block.call(proc { @tail.play_back(&block) })
end
end
And it's better that I use the system stack rather than my own because it
preserves nice behaviours with blocks
$array1 = [1, 2, 3]
$array2 = ['a', 'b', 'c']
$tail = PlaybackNil.new(nil)
$tail = $tail.record do |block|
$array1.each do |value|
$value1 = value
block.call
end
end
$tail = $tail.record do |block|
$array2.each do |value|
$value2 = value
block.call
end
end
$tail.play_back do
puts "#{$value1}, #{$value2}"
end
Above prints
1, a
2, a
3, a
1, b
2, b
3, b
1, c
2, c
3, c
I build long playback chains like this to replay different combinations of
things.
-John