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

···

--
Posted via http://www.ruby-forum.com/.

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.

···

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

--
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.

···

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

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.

--
Posted via http://www.ruby-forum.com/.

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

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.

--
Posted via http://www.ruby-forum.com/.

puts (1987..1999).select{|n| 0 == n%4 }
1988
1992
1996

···

On Oct 1, 1:05 pm, Mica Koizumi <monkeym...@yahoo.com> wrote:

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.

--
Posted viahttp://www.ruby-forum.com/.

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 Koizumi <monkeymica@yahoo.com> > 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

--
Posted via http://www.ruby-forum.com/\.

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

···

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

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

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

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

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

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

Thanks Rick -

But the part that confuses me is:

while (year1.to_i <= year2.to_i)

Shouldn't that prevent 1992 from being printed?

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 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.

-Jeremy

--
Posted via http://www.ruby-forum.com/\.

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

···

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!

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.

-Jeremy

you don't need chomp

year1 = gets.to_i
year2 = gets.to_i

-Thomas

···

On 1 October 2010 22:17, Jeremy Bopp <jeremy@bopp.net> wrote:

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

--
Thomas Preymesser
thopre@gmail.com