Ruby 新手 - IRB 中的 While 循环问题 [英] New to Ruby - While loop issues in IRB
问题描述
所以几天前我决定尝试学习 Ruby,它实际上进展得非常好.我大部分时间都在 IRB 中摆弄,直到找到一个重要的程序来编写代码来测试我的知识.
So a few days ago I decided I would try and learn Ruby and it's actually been going pretty well. I've been mostly fiddling around in IRB until I can find a non-trivial program to code to test my knowledge.
然而,今天我遇到了一个关于 While 循环的意外问题,我希望你们能帮我解决这个问题:
However, today I ran into an unexpected issue with a While loop and I was hoping y'all could help me out with this:
irb(main):001:0> i = 0
=> 0
irb(main):002:0> while (i < 1000)
irb(main):003:1> i++
irb(main):004:1* end
SyntaxError: (irb):4: syntax error, unexpected keyword_end
from C:/WINDOWS/Ruby/bin/irb:12:in `<main>'
为什么这不像我期望的那样工作?根据 本网站,我的结构正确,所以我错过了什么在这里?
Why exactly isn't this working as I'm expecting it to? According to this site I have the structure correct, so what am I missing here?
如果有帮助的话,我在 Windows XP 下运行 Ruby 1.9.2.
I'm running Ruby 1.9.2 under Windows XP, if that's of any help.
推荐答案
i++
不是有效的 ruby.你需要做 i += 1
.
i++
is not valid ruby. You need to do i += 1
.
请参阅 Mladen 关于解析器所见内容的评论.
See Mladen's comment as to what the parser is seeing.
这篇关于Ruby 新手 - IRB 中的 While 循环问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!