Ruby 1.9を使用して、列挙子の遅延評価を理解しようとしています。これは進行中の作業であるため、おそらく他のバグや不足しているコードがあるでしょうが、現在 1 つの特定の問題があります。私はこのテストに合格しようとしています (テストを変更できないことに注意してください):
def test_enumerating_with_a_single_enumerator
enumerator = SomeClass.new(some_infinite_sequence.to_enum)
assert_equal [1, 2, 3, 4, 5], enumerator.take(5)
end
以下にこのコードを書きましたが、問題は、Enumerator クラスのインスタンスである初期化メソッドの引数で SomeClass からlazy_selectインスタンス メソッドを呼び出していることだとわかっているため、NoMethodError が発生します。助言がありますか?ありがとうございました。
class SomeClass < Enumerator
def initialize(*enumerators)
super() do |yielder|
enumerators.each do |enumerator|
enumerator.lazy_select { |yielder, first_value, second_value| yielder.yield first_value if (first_value <=> second_value) <= 0 }
.first(20)
end
end
end
def lazy_select(&block)
self.class.new do |yielder|
each_cons(2) do |first_value, second_value|
block.call(yielder, first_value, second_value)
end
end
end
end