简体   繁体   English

Rubinius:如何生成枚举器作为官方方式?

[英]Rubinius: how to generate enumerator as the official way?

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. 在官方Ruby 1.9.3中,输出是[1,2,3,4,5] ,这就是我想要的。

But in Rubinius, it gives error and tells me cannot find Enumerator constant. 但是在Rubinius中,它给出了错误并且告诉我找不到Enumerator常量。

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 所以我查了一下,发现Enumerator在Enumerable模块而不是kernel定义,当它生成时,它需要括号中的一些参数: http//rubydoc.info/github/evanphx/rubinius/master/Enumerable/枚举

I tried to change Enumerator.new to Enumerable::Enumerator.new , or include Enumerable , neither works because it needs more arguments. 我试图将Enumerator.new更改为Enumerable::Enumerator.new ,或者include Enumerable ,因为它需要更多参数,所以无法工作。

How can I do the example above in Rubinius? 我如何在Rubinius中做上面的例子? Is there any way around to make the code work in both official and Rubinius? 有没有什么方法可以让代码在官方和Rubinius中运行?

You're using Rubinius in 1.8 mode, which doesn't have Enumerator in the global namespace. 你在1.8模式下使用Rubinius,它在全局命名空间中没有Enumerator。 Please use Rubinius in 1.9 mode and the example works fine then. 请在1.9模式下使用Rubinius,然后示例工作正常。 You can use 1.9 by passing -X19 when starting Rubinius, or setting RBXOPT=-X19 for example. 你可以在启动Rubinius时通过-X19使用1.9,或者设置RBXOPT = -X19。

It's also possible to make 1.9 mode the default with configure during compile time. 在编译期间,还可以使用configure将1.9模式设置为默认模式。

Sounds like a bug/missing class in Rubinius. 听起来像Rubinius中的一个错误/缺少类。 Open up an issue on github and it will get added. 在github上打开一个问题,它会被添加。 Or dig in and send a pull request! 或者挖掘并发送拉动请求!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM