Created
March 31, 2016 22:57
-
-
Save nepalez/4d0a82c527cbfa91fbbd2ad6d583a464 to your computer and use it in GitHub Desktop.
Martian anniversaries
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
# Happy birthday to you... | |
# | |
# @example | |
# dates = MartianAnniversaries.from '2015-01-01' | |
# dates.take(2) | |
# # => ['2016-11-17', '2018-10-05'] | |
# | |
class MartianAnniversaries | |
include Enumerable | |
attr_reader :birthday | |
def self.from(str) | |
new(str) | |
end | |
def initialize(str) | |
@birthday = Date.parse(str).to_time | |
end | |
def each | |
to_enum unless block_given? | |
yield format(@anniversary = birthday + martian_year) | |
loop { yield format(@anniversary += martian_year) } | |
end | |
private | |
def format(seconds) | |
Time.at(seconds).to_date.strftime '%Y-%m-%d' | |
end | |
def martian_day | |
@martian_day ||= 88775.244 | |
end | |
def martian_year | |
@martian_year ||= 668.5907 * martian_day | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment