From: dblack@... Date: 2006-11-08T05:33:11+09:00 Subject: Re: Help with a program to determin leap years. Hi -- On Wed, 8 Nov 2006, Shiloh Madsen wrote: > So, I'm trying to go through the Teach Yourself Programming book by > Pragmatic Press and I am encountering a few hurdles. The chapter I am > working on now is asking me to create a program which will ask for a > start and end year and then calculate all leap years in that range. > The logic behind leap years (for those who need a refresher) is all > years divisible by for are leap years EXCEPT those that are divisible > by 100 UNLESS they are also divisible by 400. I am somewhat at a loss > for how to handle the logic for this...finding all numbers that are > divisible by 4 and removing those divisible by 100 is easy. Its adding > in that third condition which adds some of the removed numbers back > into the "true" group that I am having trouble with...or maybe I am > just not wrapping my mind around the problem well > enough...suggestions? require 'date' Date.leap?(year) # :-) See Morton's implementation. Here, just for fun, is another: def leap?(year) year % 4 == 0 unless (year % 100 == 0 unless year % 400 == 0) end It returns nil/true rather than false/true, so it's a bit non-slick. But I thought the semantics might be interesting. David -- David A. Black | dblack@wobblini.net Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org