Today’s Ruby Tuesday is on Date#leap?.
Date#leap? operates on a
Date object, and returns
true if the year is a leap year in the Gregorian calender, or
false if not.
Date.new(2015, 1, 4).leap? => false Date.new(2012, 1, 1).leap? # => true Date.new(2017, 1, 1).leap? # => false Date.new(2000, 1, 1).leap? # => true Date.new(1900, 1, 1).leap? # => false
Date#leap?, you don’t have to code the rules to check if a year is a leap year, or even have to double check the rules about when the century is a leap year.
There is also a class level version
Date::leap?, that will take a integer value for the year, and return a
Date.leap? 2015 # => false Date.leap? 2012 # => true Date.leap? 2017 # => false Date.leap? 2000 # => true Date.leap? 1900 # => false
Both the instance and class level versions of ‘leap?` can handle Year Zero, and negative years.
Date.leap? -1 # => false Date.leap? -4 # => true Date.leap? 0 # => true Date.new(-4, 1, 1).leap? # => true
I wouldn’t know if that is valid myself, but that is why it is useful to have this a built in method on
Date instead of having to code it up myself.