Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. require 'date'
  2.  
  3. def days_until_xmas(today = Date.today)
  4. # TODO Return the number of days before Christmas
  5.  
  6. # Determine the christmas date
  7. christmas = Date.new(today.year, 12, 25)
  8.  
  9. # Change the year of christmas if today is passed the 25th
  10. if today > christmas
  11. christmas = Date.new(today.year+1, 12, 25)
  12. end
  13.  
  14. # Count the number of days between christmas day and today
  15. days_remaining = christmas - today
  16.  
  17. # Return the results as an integer
  18. return days_remaining.to_i
  19. end
  20.  
  21. puts days_until_xmas()
  22.  
  23. # We want to display "true" to test our method (TDD)
  24. puts days_until_xmas(Date.today).class == Integer
  25.  
  26. puts days_until_xmas(Date.today) == 169 # As of 2019, 07, 09
  27. puts days_until_xmas(Date.new(2018,12,25)) == 0
  28. puts days_until_xmas(Date.new(2020,12,26)) == 364
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement