Skip to content

Instantly share code, notes, and snippets.

@krokrob
Created October 8, 2019 15:45
Show Gist options
  • Save krokrob/4f433621f27a8ef4a1e853ec8c156211 to your computer and use it in GitHub Desktop.
Save krokrob/4f433621f27a8ef4a1e853ec8c156211 to your computer and use it in GitHub Desktop.
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