fork download
  1. class Count
  2. attr_accessor :x
  3. def initialize n
  4. @x = "test" # アクセスしたい変数
  5. @func = ->{ n+=1 }
  6. end
  7. def call
  8. @func.call
  9. end
  10. end
  11.  
  12. class Yakumo_yukari < Enumerator
  13. attr_accessor :enum
  14. attr_accessor :cc
  15. def initialize n = 0
  16. y = Enumerator.new do | e |
  17. @cc = Count.new n
  18. loop do
  19. e.yield @cc.call
  20. end
  21. end
  22. @enum = super y
  23. end
  24. end
  25. y = Yakumo_yukari.new 5
  26. p y.next
  27. p y.next
  28. p y.rewind
  29. p y.next
  30. p y.take(20)
  31.  
  32. p y.cc.x
  33.  
Success #stdin #stdout 0s 4852KB
stdin
Standard input is empty
stdout
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"