Ruby クロージャでインスタンス変数を捕捉する
久しぶりにRuby書いた
ググってもカスだったのでメモる
class Test def initialize @count = 0 end def closure Proc.new { @count += 1 puts @count } end end t = Test.new c = t.closure 10.times do c.call end
$ ruby test.rb 1 2 3 4 5 6 7 8 9 10
久しぶりにRuby書いた
ググってもカスだったのでメモる
class Test def initialize @count = 0 end def closure Proc.new { @count += 1 puts @count } end end t = Test.new c = t.closure 10.times do c.call end
$ ruby test.rb 1 2 3 4 5 6 7 8 9 10