- 43 名前:デフォルトの名無しさん mailto:sage [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?
|

|