Simon Strandgaard
11/21/2003 11:28:00 AM
On Fri, 21 Nov 2003 19:53:47 +0900, ts wrote:
>
> Try this (not tested)
>
> def marshal_dump
> def marshal_load(arr)
Above partially solves it. However adding a position count, then it fully
works. Thanks.
Cloning continuations seems not to be possible ?
irb(main):001:0> x = false
=> false
irb(main):002:0> callcc{|x| }
=> nil
irb(main):003:0> p x
#<Continuation:0x81cba80>
=> nil
irb(main):004:0> y = x.clone
NoMethodError: allocator undefined for Continuation
from (irb):4:in `clone'
from (irb):4
Therefore when doing cloning, I must iterate until @position are reached.
Any idea if this skip-until-position code can be reduced?
--
Simon Strandgaard
server> ruby a.rb
"l"
"o"
server> expand -t2 a.rb
class Object
def deep_clone
Marshal::load(Marshal.dump(self))
end
end
class IteratorContinuation
def initialize(instance, symbol, position=nil)
@instance = instance
@symbol = symbol
@position = position || 0
first
@position.times { self.next } # TODO: can this be avoided ?
end
def first
@value = nil
@resume_where = false
@return_where = Proc.new{}
@instance.method(@symbol).call {|i|
@value = i
callcc{|@resume_where|
@return_where.call
return
}
}
@resume_where = false
@return_where.call
end
def next
@position += 1
callcc{|@return_where| @resume_where.call }
end
def is_done?
@resume_where == false
end
def current
@value
end
def clone
self.class.new(@instance, @symbol, @position)
end
def marshal_dump
[@instance, @symbol, @position]
end
def marshal_load(arr)
initialize(*arr)
end
end
i1 = IteratorContinuation.new("hello world", :each_byte)
i1.next
i1.next
i1.next
i2 = i1.clone
#i2 = i1.deep_clone
i2.next
p i1.current.chr #=> "l"
p i2.current.chr #=> "o"
server>