迭代器的无限产量
我正在尝试学习一些红宝石。 想象一下,我正在循环并执行一个长时间运行的过程,在这个过程中,我希望获得一个旋转器,只要需要就可以。
所以我可以这样做:
a=['|','/','-','\\']
aNow=0
# ... skip setup a big loop
print a[aNow]
aNow += 1
aNow = 0 if aNow == a.length
# ... do next step of process
print "\b"
但我认为这样做会更干净:
def spinChar
a=['|','/','-','\\']
a.cycle{|x| yield x}
end
# ... skip setup a big loop
print spinChar
# ... do next step of process
print "\b"
当然 spinChar 调用需要一个块。 如果我给它一个块,它就会无限期地挂起。
我怎样才能得到这个区块的下一个产量?
I'm trying to learn some ruby.
Imagine I'm looping and doing a long running process, and in this process I want to get a spinner for as long as necessary.
So I could do:
a=['|','/','-','\\']
aNow=0
# ... skip setup a big loop
print a[aNow]
aNow += 1
aNow = 0 if aNow == a.length
# ... do next step of process
print "\b"
But I thought it'd be cleaner to do:
def spinChar
a=['|','/','-','\\']
a.cycle{|x| yield x}
end
# ... skip setup a big loop
print spinChar
# ... do next step of process
print "\b"
Of course the spinChar call wants a block. If I give it a block it'll hang indefinitely.
How can I get just the next yeild of this block?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我喜欢所有这些建议,但我在标准库中找到了生成器,我认为它更符合我想要做的事情:
在冻结数组上用模数增加的普通数组索引似乎是最快。
弗拉德的帖子很漂亮,但不完全是我想要的。 ),单行增量会更好
在
spinner 类
中,如果 Ruby 支持i++
(如GLYPHS[@i++%GLYPHS.length]
Max 的>带有推移的旋转闭包
对我来说似乎有点密集,但生成的语法几乎与这个生成器完全相同。 至少我认为这是一个带有 proc 的闭包。Chuck 的
with_spinner
实际上非常接近我想要的,但是如果您不必像上面那样使用生成器,为什么要中断呢?Vadim,感谢您指出
生成器
会很慢。I like all these suggestions, but I found the Generator in the standard library, and I think it's more along the lines of what I wanted to do:
Plain
array index
increments with modulus on a frozen array seems to be fastest.Vlad's thread is nifty but not exactly what I wanted. And in
spinner class
the one-line increment would be nicer if Ruby supportedi++
likeGLYPHS[@i++%GLYPHS.length]
Max's
spinner closure
with push shift seems a little intensive to me, but the resulting syntax is almost exactly like this Generator. At least I think that's a closure with proc in there.Chuck's
with_spinner
is actually pretty close to what I wanted, but why break if you don't have to with a Generator as above.Vadim, thanks for pointing out the
generator
would be slow.我认为您的
cycle
走在正确的轨道上。 像这样的事情怎么样:I think you were on the right track with
cycle
. How about something like this:我认为您不太了解 Ruby 中的 yield 的作用。 它不会从块返回值 - 它将值传递给您传递给封闭方法的块。
我想你想要更多这样的东西:
I don't think you quite understand what
yield
does in Ruby. It doesn't return a value from a block — it passes a value to the block you've passed to the enclosing method.I think you want something more like this:
曾几何时,我写了一个数组。 但它不仅仅是一个数组,它是一个有指针的数组,所以你可以调用 nextforeverrr!
http://gist.github.com/55955
将此类与简单的迭代器或循环配对,您就可以是金色的。
Once upon a time, I wrote an array. But it's not just an array, it's an array that has a pointer, so you can call next foreverrr!
http://gist.github.com/55955
Pair this class with a simple iterator or loop and you are golden.
如果您能原谅我这么说的话,您的代码有点由内而外。 :)
为什么不:
现在,如果你想要这样的东西:
...那么你必须使用多线程:
Your code is a bit inside-out, if you'll pardon me saying so. :)
Why not:
Now, if you want something like:
...then you'd have to use multi-threading:
呵呵,我上面的答案都是脏话。
hehe, the answer above mine is all dirty.
Ruby 的
yield
无法按照您的方式工作例子会喜欢。 但这可能是一个闭包的好地方:实际上,我认为这解决方案本身可能太“聪明”,但要理解
Proc< 的想法无论如何, /code>
可能会有用。
Ruby's
yield
does not work in the way your example would like. But this might be a good place for a closure:In practice I think this solution might be too "clever" for its own good, but understanding the idea of
Proc
s could be useful anyhow.