Skip to content

Instantly share code, notes, and snippets.

@carwin
Last active December 20, 2015 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carwin/6208000 to your computer and use it in GitHub Desktop.
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.
# 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