New to Ruby, Looking for Help With Basic Program

M

Mica Koizumi

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.

Thanks in advance...
 
J

Justin Collins

-----Original Message-----
From: (e-mail address removed) [mailto:[email protected]]
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.

Thanks in advance...
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
 
J

Jeremy Bopp

Thanks Rick -

But the part that confuses me is:

while (year1.to_i <= year2.to_i)

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
 
M

Mica Koizumi

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!
 
J

Jared Miller

[Note: parts of this message were removed to make it a legal post.]

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!'
 
A

Adriano Ferreira

[Note: parts of this message were removed to make it a legal post.]

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.

Adriano



________________________________
From: Mica Koizumi <[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.

Thanks in advance...
 
T

Thomas Preymesser

[Note: parts of this message were removed to make it a legal post.]

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
you don't need chomp

year1 = gets.to_i
year2 = gets.to_i

-Thomas
 
W

w_a_x_man

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.

Thanks in advance...

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top