I have this simple code to generate a lazy array:

lazy_arr = Enumerator.new { |y|
    i = 1
    loop {
        y << i
        i+=1
    }
}
p lazy_arr.take(5)

In official Ruby 1.9.3, the output is [1,2,3,4,5], which is what I want.

But in Rubinius, it gives error and tells me cannot find Enumerator constant.

So I looked it up, and find Enumerator defined in Enumerable module instead of kernel, and when it is generated, it needs a few arguments in the brackets: http://rubydoc.info/github/evanphx/rubinius/master/Enumerable/Enumerator

I tried to change Enumerator.new to Enumerable::Enumerator.new, or include Enumerable, neither works because it needs more arguments.

How can I do the example above in Rubinius? Is there any way around to make the code work in both official and Rubinius?

有帮助吗?

解决方案

You're using Rubinius in 1.8 mode, which doesn't have Enumerator in the global namespace. Please use Rubinius in 1.9 mode and the example works fine then. You can use 1.9 by passing -X19 when starting Rubinius, or setting RBXOPT=-X19 for example.

It's also possible to make 1.9 mode the default with configure during compile time.

其他提示

Sounds like a bug/missing class in Rubinius. Open up an issue on github and it will get added. Or dig in and send a pull request!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top