Last active
December 20, 2015 22:49
-
-
Save carwin/6208000 to your computer and use it in GitHub Desktop.
Ruby method that tells you how many minutes are in a given year.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
# Write a method leap_year?. It should accept a year value from the user, check | |
# whether it is a leap year and return true or false. | |
# Calculate and display the number of minutes in a leap year (2000 and 2004) | |
# and the minutes in a non-leap year (1900 and 2005). | |
MINUTES_IN_HOUR = 60 | |
MINUTES_IN_DAY = MINUTES_IN_HOUR * 24 | |
def leap_year? | |
puts "Give me a year, I'll tell you if it leaps:" | |
year = gets | |
yield(year) | |
end | |
leap_year? do |year| | |
is_leap = case | |
when year.to_f % 400 == 0 then true | |
when year.to_f % 100 == 0 then false | |
else year.to_f % 4 == 0 | |
end | |
elsif is_leap == false | |
puts "#{year} is not a leap year. Normal years have " + (MINUTES_IN_DAY * 365).to_s + " minutes total." | |
else | |
puts "#{year} is a leap year and leap years have " + (MINUTES_IN_DAY * 366).to_s + " minutes total." | |
end | |
exit | |
end | |
leap_year? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment