callすると自分自身を返すProcオブジェクト
ハマったので。出来たのも偶然。
やり方はこんな感じ。引数を1つ取るProcオブジェクトの場合。
f = Proc.new{ t = Proc.new{|x| t} }.call
これでこんな事になる。
f = Proc.new{ t = Proc.new{|x| t} }.call p f # <Proc:0x100f19c0@> 5.times do f = f.call(nil) p f # <Proc:0x100f19c0@> end f = f.call(0).call(1).call(2).call(3) p f # <Proc:0x100f19c0@> f = f[7][38][nil][$stdin][false][Object]["bar"][f] p f # <Proc:0x100f19c0@>
びくともしない。
外のProcでのtがコンテキストとして内側のProcから見えるから、こう書けると。
最初にcallを入れないといけないのがかっこ悪いけど。
ちなみに、引数の個数は
f0 = Proc.new{ t = Proc.new{ t} }.call # f0 == f0.call() f1 = Proc.new{ t = Proc.new{|x| t} }.call # f1 == f1.call(nil) f2 = Proc.new{ t = Proc.new{|x, y| t} }.call # f2 == f2.call(nil, nil)
みたいに変えられる。