Forum: Ruby Determine whether year is leap year or not

87985e2e3e065f6c899ef563d0dd3b52?d=identicon&s=25 Ryan Glass (ryanoglass)
on 2016-09-26 01:16
I have to determine whether a year is a leap year or not when the user
inputs the year.

Example below:


Enter year: 2016
2016 is a leap year

Enter year: 2013
2013 is not a leap year

I have no idea where to start as my professor did not explain it very
well in his notes. Can somebody please help me.
6c7fb11d4f3fa354a51a3ea0375d895f?d=identicon&s=25 Joe Gain (timeout)
on 2016-09-26 08:29
Ryan, you go to wikipedia and find out how to calculate whether or not a
year is a leap year. Then you implement it in ruby. That's how you're
going to learn the most from the task.

Hint: You'll probably need a class with a method: is_leap_year? After
that's working you can think about printing the message to the terminal.
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2016-09-29 11:34
Joe Gain wrote in post #1185078:
> Hint: You'll probably need a class with a method: is_leap_year? After
> that's working you can think about printing the message to the terminal.

I would not create a class just for this. What instance variables should
the class have? Instead I would do one of the following:

(1) Put the method is_leap_year? into Fixnum

     class Fixnum
        def is_leap_year?
          ...
        end
     end

(2) Likewise, put the method into Time and name it then more
appropriately
is_in_leap_year?

(3) Create a module for this - for instance DateUtilities - and make it
a module method, like this:

    module DateUtilities

      extend self

      def is_leap_year?(year)
        ...
      end

    end
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.