Learn and Learn

Learn And Learn - great place for tutorials, references and how-to

Ruby – Get Leap Year using leap Method – Examples & Explanation

The leap?() method returns true if the year in the given parameter is a leap year. The leap year comes after every 4 years with some other certain checks. To get the leap year you can use Date.leap?() method.

leap?() Method in Ruby

leap?() method exists in Standard Date Library of Ruby. The purpose of this method is to get leap year from Calendar.

 

Syntax of the leap?() Method in Ruby

In date, the syntax of leap?() method is:

 

Parameter of leap?() Method in Ruby

The parameter x is any valid date object. This parameter is required.Note: If the x parameter is not a valid date object leap?() method will return an error.

 

Return Value of leap?()

leap?() method return the true if the year mentioned in the code is a leap year.

 

Ruby – Get leap?() Year Example 1


Output
true

 

Ruby – Get leap?() Year Example 2


Output
false

 

Ruby – Get leap?() Year Example 3


Output
false

 

Ruby – Get leap?() Year Example 4


Output
true

 

LearnAndLearn.com © 2018 - All rights reserved