New to Ruby - While loop issues in IRB

前端 未结 3 725
失恋的感觉
失恋的感觉 2020-12-20 06:51

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 pr

相关标签:
3条回答
  • 2020-12-20 07:29

    Unless your purpose is specifically to understand how while loops and integer addition/comparison work, what you really want is

    1000.times do |i|
    end
    
    0 讨论(0)
  • 2020-12-20 07:31

    Ruby doesn't have C-style increment (++) or decrement (--) operators. You want this:

    i = 0
    while(i < 1000)
      i = i + 1 # Or i += 1
    end
    
    0 讨论(0)
  • 2020-12-20 07:40

    i++ is not valid ruby. You need to do i += 1.

    Edit: See Mladen's comment as to what the parser is seeing.

    0 讨论(0)
提交回复
热议问题