Created
October 8, 2019 15:45
-
-
Save krokrob/4f433621f27a8ef4a1e853ec8c156211 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require "date" | |
def days_before_xmas(year = nil, month = nil, day = nil) | |
# set today and store it in a variable | |
if year.nil? | |
today = Date.today | |
year = today.year | |
else | |
today = Date.new(year, month, day) | |
end | |
# set xmas date and store it in a variable | |
if month == 12 && day > 25 | |
xmas_date = Date.new(year + 1, 12, 25) | |
else | |
xmas_date = Date.new(year, 12, 25) | |
end | |
# xmas - today and store it in a variable | |
days_before_xmas = (xmas_date - today).to_i | |
# return difference | |
return days_before_xmas | |
end | |
puts "It is #{days_before_xmas} days before christmas, hurry up!" | |
# tests | |
puts days_before_xmas(2019, 10, 8).class == Integer | |
puts days_before_xmas(2019, 10, 8) == 78 | |
puts days_before_xmas(2019, 10, 9) == 77 | |
puts days_before_xmas(2020, 10, 9) == 77 | |
puts days_before_xmas(2019, 12, 27) == 364 | |
puts days_before_xmas == 78 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment