1

I am going through the Ruby tutorials from Learn to Program, and I am having a problem with a while loop.

This is what I want to do:

while y_now % 4
    y_now += 1
    puts y_now % 4
end
gets

I'm using the gets just to pause execution so I can see the output. (This is looking for a leap year, btw)

Anyways, when y_now % 4 = 0, the while loop continues, and I don't understand why, since this is different from all my previous experience.

I don't want someone to just fix my code though, I really want to understand why this happens. I've obviously just started Ruby, so whatever help I get is very much appreciated.

Note: This seems to be similar to this question, but with integers instead of strings.

Community
  • 1
  • 1
Paul Hoffer
  • 12,606
  • 6
  • 28
  • 37
  • Same answer as [How to make Ruby variable that will evaluate to false in conditional expressions](http://stackoverflow.com/questions/2152979/how-to-make-ruby-variable-that-will-evaluate-to-false-in-conditional-expressions) – Marc-André Lafortune Jul 22 '10 at 02:29
  • That's the question I linked to, I just couldn't tell if that extended to mathematical expressions or just strings. (Still new to Ruby) – Paul Hoffer Jul 22 '10 at 02:42
  • I should have linked to http://stackoverflow.com/questions/3082341/ifx-vs-ifxfalse-in-ruby/3082399#3082399 – Marc-André Lafortune Jul 22 '10 at 03:19
  • @Marc-André Lafortune Your answer there was incredibly helpful! Thank you! – Paul Hoffer Jul 22 '10 at 03:39

1 Answers1

4

In Ruby only the nil object and a special false object are "false", everything else (including the integer 0) is "true". You should use while (y_now % 4) != 0.

Marc-André Lafortune
  • 78,216
  • 16
  • 166
  • 166
Chris B.
  • 85,731
  • 25
  • 98
  • 139
  • OK I got it working now. I am having so much trouble because anytime I have an error, it just exits the command line immediately. Is there any way to have it spit out the error before exiting? – Paul Hoffer Jul 22 '10 at 02:36
  • @phoffer - outputting the error is default behavior, so absence of one might indicate that the script completed successfully without actually doing anything... – Matchu Jul 22 '10 at 02:44
  • @Matchu I am putting a gets at the end of my scripts for the time being so that it pauses before exiting. When it doesn't get there it just exits. I am opening the files from explorer, launching the command line interface. Should I open the command line first and run them through it that way? – Paul Hoffer Jul 22 '10 at 02:46
  • Wow, why didn't I think of that before? Answered my own question just now on my own. Thanks Matchu. Is there a way to have it output the error without exiting if I launch them from explorer? – Paul Hoffer Jul 22 '10 at 02:48
  • @phoffer - ahh, there ya go. I've run into this numerous times for bash scripts and the like - it hits an error, but the window disappears before I can read it, since the program is technically finished. Glad to help xD As far as a fix goes, there's probably not a clean way, but maybe you can be creative :/ – Matchu Jul 22 '10 at 02:49
  • Maybe after I get used to putting a 20 line script together without 8 revisions :) I do like it though (used to php), just gotta get used to it. – Paul Hoffer Jul 22 '10 at 02:52