# 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:

puts ‘Year 1?’
year1 = gets.chomp
puts ‘Year 2?’
year2 = gets.chomp

while (year1.to_i <= year2.to_i)
if year1.to_i % 4 == 0
leapyear = year1.to_i + 4
puts leapyear
end
if year1.to_i % 4 != 0
year1 = year1.to_i + 1
puts year1
end
end

When year1 = 1987 and year2 = 1990, why is it printing out 1992? I am
expecting 1988.

On Fri, Oct 1, 2010 at 2:05 PM, Mica K. [email protected]
wrote:

if year1.to_i % 4 == 0
leapyear = year1.to_i + 4
puts leapyear
end

This says 'paraphrasing"

if year1 is a leapyear then print year1 + 4

1988 is the only leap year between 1987 and 1990.

1988 + 4 is 1992

BTW, it’s not enough for a year to be divisible by 4 to be a leap year.

Years divisible by 100 are not leap years (even though they are all
divisible by 4) unless they are also divisible by 400.

Rick DeNatale

Github: rubyredrick (Rick DeNatale) · GitHub
WWR: http://www.workingwithrails.com/person/9021-rick-denatale

Thanks Rick -

But the part that confuses me is:

while (year1.to_i <= year2.to_i)

Shouldn’t that prevent 1992 from being printed?

Also - re: yeap year being divisible by 100 - I do plan on adding that
in after I get past this hump. Thanks for the input.

Rick Denatale wrote:

On Fri, Oct 1, 2010 at 2:05 PM, Mica K. [email protected]
wrote:

if year1.to_i % 4 == 0
ï¿½leapyear = year1.to_i + 4
ï¿½puts leapyear
end

This says 'paraphrasing"

if year1 is a leapyear then print year1 + 4

1988 is the only leap year between 1987 and 1990.

1988 + 4 is 1992

BTW, it’s not enough for a year to be divisible by 4 to be a leap year.

Years divisible by 100 are not leap years (even though they are all
divisible by 4) unless they are also divisible by 400.

Rick DeNatale

Github: rubyredrick (Rick DeNatale) · GitHub
WWR: http://www.workingwithrails.com/person/9021-rick-denatale

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.

DaShiell, Jude T. CIV NAVAIR 1490, 1, 26 wrote:

puts ‘Year 1?’
year1 = year1.to_i + 1

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.
That is completely backwards. The OP has it right.

-Justin

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!

Thanks again!

Jeremy B. wrote:

On 10/01/2010 01:29 PM, Mica K. 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.

-Jeremy

On 10/01/2010 01:29 PM, Mica K. 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.

-Jeremy

On 10/01/2010 03:00 PM, Mica K. wrote:

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.

-Jeremy

Hi Mica,

Now it does, with some refactoring:

year1 = 1987
year2 = 1990

puts (year1 % 4 == 0 ? year1 + 4 : year1 + 1) while year1 <= year2

# => 1988 …

Hope it helped.

From: Mica K. [email protected]
To: ruby-talk ML [email protected]
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:

puts ‘Year 1?’
year1 = gets.chomp
puts ‘Year 2?’
year2 = gets.chomp

while (year1.to_i <= year2.to_i)
if year1.to_i % 4 == 0
leapyear = year1.to_i + 4
puts leapyear
end
if year1.to_i % 4 != 0
year1 = year1.to_i + 1
puts year1
end
end

When year1 = 1987 and year2 = 1990, why is it printing out 1992? I am
expecting 1988.

On 1 October 2010 22:17, Jeremy B. [email protected] wrote:

year2 = gets.chomp.to_i

you don’t need chomp

year1 = gets.to_i
year2 = gets.to_i

-Thomas

Mica, are you going through the book Learn to Program by Chris P.?
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 Oct 1, 1:05 pm, Mica K. [email protected] wrote:

leapyear = year1.to_i + 4