class Count attr_accessor :x def initialize n @x = "test" # アクセスしたい変数 @func = ->{ n+=1 } end def call @func.call end end class Yakumo_yukari < Enumerator attr_accessor :enum attr_accessor :cc def initialize n = 0 y = Enumerator.new do | e | @cc = Count.new n loop do e.yield @cc.call end end @enum = super y end end y = Yakumo_yukari.new 5 p y.next p y.next p y.rewind p y.next p y.take(20) p y.cc.x
Standard input is empty
6 7 #<Yakumo_yukari: #<Enumerator: #<Enumerator::Generator:0x909aac0>:each>:each> 6 [6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25] "test"