The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

[Newbie!] Ruby leap year problem.

I'm trying to learn ruby, and everything is going great. I am enjoying it as a language, and I find using the command prompt far easier than having to upload every document, like I did with PHP. I'm using the tutorial on Pine.fm, and I've reached Chapter 6 without a problem. Chapter 6 is on flow control, and one of the ending exercises is:

Leap Years. Write a program which will ask for a starting year and an ending year, and then puts all of the leap years between them (and including them, if they are also leap years). Leap years are years divisible by four (like 1984 and 2004). However, years divisible by 100 are not leap years (such as 1800 and 1900) unless they are divisible by 400 (like 1600 and 2000, which were in fact leap years). (Yes, it's all pretty confusing, but not as confusing has having July in the middle of the winter, which is what would eventually happen.)

And this is what I've done:

Code:

puts 'This program will determine what leap years are between two specific dates that you assign.'
puts ''
puts 'What would you like your starting year to be?'
start = gets.chomp
puts 'This program will start at ' + start + '.'
puts ''
puts 'What would you like your ending year to be?'
close = gets.chomp
puts 'This program will end at ' + close + '.'
# Checks if the closing date is before the starting date
if close < start
puts 'Your closing date is before your start date. Please try again'
else
puts 'Calculating...'
end
# Is the starting date a leap year?
if (start.to_i/4 or start.to_i/400)
puts 'Your starting date ' + start + ' is a leap year!'
end

To finish it, I'm guessing I need to loop with a while statement and add 4 to the first leap year inside the range, until it reaches the end date. Can anyone help? I've tried a few combinations, and read through looping again, but I can't quite grasp it.

puts 'This program will determine what leap years are between two specific dates that you assign.'
puts ''
puts 'What would you like your starting year to be?'
start_year = gets.chomp.to_i
puts ''
puts 'What would you like your ending year to be?'
end_year = gets.chomp.to_i
puts "Leap years between #{start_year} and #{end_year}"
if start_year > end_year
puts 'Your closing date is before your start date. Please try again'
return
end
leap_years = []
start_year.upto(end_year) do |year|
if (year % 4 == 0)
leap_years << year unless (year % 100 == 0) and (year % 400 != 0)
end
end
puts leap_years.join(', ')

Some notes:
- you can use the Integer#upto method to step through each year (see http://www.ruby-doc.org/core/ for API docs)
- remember to cast the user input into integers (and add error-handling, which I have left out in the code above)
- the % operator returns the remainder... E.g. 10 % 3 gives you 1.