Probably because chomp.gets will work better. The gets.chomp has has chomp getting first crack at info and then chomp passes it along to gets.
···
-----Original Message-----
From: monkeymica@yahoo.com [mailto:monkeymica@yahoo.com]
Sent: Friday, October 01, 2010 14:05
To: ruby-talk ML
Subject: New to Ruby, Looking for Help With Basic Program
I am teaching myself Ruby and am trying to figure out why this program
is doing what it is doing:
________________________________
From: Mica Koizumi <monkeymica@yahoo.com>
To: ruby-talk ML <ruby-talk@ruby-lang.org>
Sent: Fri, October 1, 2010 3:05:20 PM
Subject: New to Ruby, Looking for Help With Basic Program
I am teaching myself Ruby and am trying to figure out why this program
is doing what it is doing:
-----Original Message-----
From: monkeymica@yahoo.com [mailto:monkeymica@yahoo.com] Sent: Friday, October 01, 2010 14:05
To: ruby-talk ML
Subject: New to Ruby, Looking for Help With Basic Program
I am teaching myself Ruby and am trying to figure out why this program
is doing what it is doing:
That while condition is only evaluated at the beginning of each pass
through the loop. The first pass (where 1987 <= 1900) adds 1 to year1
making it 1988. The second pass (where 1988 <= 1990) sets *leapyear* to
year1 + 4 (1988 + 4 = 1992) and then prints *leapyear* which is now
1992; however, it does not modify the value of year1. From hear on, the
loop continues printing 1992 infinitely because year1 is never updated
again and will always be 1988 which is less than 1990.
-Jeremy
···
On 10/01/2010 01:29 PM, Mica Koizumi wrote:
Rick Denatale wrote:
On Fri, Oct 1, 2010 at 2:05 PM, Mica Koizumi <monkeymica@yahoo.com> >> wrote:
if year1.to_i % 4 == 0
�leapyear = year1.to_i + 4
�puts leapyear
end
and that helped a great deal. I see now that I was not evaluating the
incrementation.
You guys have been a great help!
Thanks again!
Jeremy Bopp wrote:
···
On 10/01/2010 01:29 PM, Mica Koizumi wrote:
if year1 is a leapyear then print year1 + 4
Shouldn't that prevent 1992 from being printed?
That while condition is only evaluated at the beginning of each pass
through the loop. The first pass (where 1987 <= 1900) adds 1 to year1
making it 1988. The second pass (where 1988 <= 1990) sets *leapyear* to
year1 + 4 (1988 + 4 = 1992) and then prints *leapyear* which is now
1992; however, it does not modify the value of year1. From hear on, the
loop continues printing 1992 infinitely because year1 is never updated
again and will always be 1988 which is less than 1990.
Mica, are you going through the book *Learn to Program* by Chris Pine?
That's what I'm going through as well, and he has you do that exact same
program.
puts 'What is the starting year?'
starting = gets.chomp.to_i
puts 'What is the ending year?'
ending = gets.chomp.to_i
puts 'Here is your list of leap years!'
year = starting
while year <= ending
if year%4 == 0
if year%100 != 0 || year%400 == 0
puts year
end
end
year = year + 1
end
puts 'Done!'
···
---
Jared Miller
On Fri, Oct 1, 2010 at 4:17 PM, Jeremy Bopp <jeremy@bopp.net> wrote:
On 10/01/2010 03:00 PM, Mica Koizumi wrote:
> Thank you Jeremy! I changed:
>
> if year1.to_i % 4 == 0
> leapyear = year1.to_i + 4
> puts leapyear
>
> to:
>
> if year1.to_i % 4 == 0
> year1 = year1.to_i + 4
> puts year1
>
>
> and that helped a great deal. I see now that I was not evaluating the
> incrementation.
>
> You guys have been a great help!
I have another suggestion to help simplify your code a bit. Replace
these lines:
year1 = gets.chomp
year2 = gets.chomp
With:
year1 = gets.chomp.to_i
year2 = gets.chomp.to_i
Now you can drop all the .to_i business elsewhere in this code since
year1 and year2 will be integers from the beginning.