Rubyは難しすぎ
■ このスレッドは過去ログ倉庫に格納されています
0043デフォルトの名無しさん
2006/09/24(日) 23:16:11本スレでcallccを使えば内部イテレータを外部イテレータに出来そうだと聞いて、やったら出来たワロタ。
class Iterator
attr_reader :item, :retval
def initialize(it)
@p = lambda {|*args| @c_.call args if callcc {|@c| true }}
@item = callcc {|@c_|
@retval = it.call &@p
@c_.call nil
}
end
def has?
return @item != nil
end
def next
v = @item
@item = callcc {|@c_| @item = @c.call }
return v
end
end
a = [666, nil, "unko", :+]
it = Iterator.new a.method(:each)
puts it.next while it.has?
■ このスレッドは過去ログ倉庫に格納されています