Skip to content

Instantly share code, notes, and snippets.

@burtlo
Created February 26, 2013 16:31
Show Gist options
  • Save burtlo/5039860 to your computer and use it in GitHub Desktop.
Save burtlo/5039860 to your computer and use it in GitHub Desktop.
Space Ages - functional style
require 'date'
# puts "What are you? #{ARGV} #{ARGV.class}"
# Called as in `ruby example2.rb 1977 10 30`
# birthdate = ARGV.map {|arg| arg.to_i }
birthdate = [ 1977, 10, 30 ]
# year, month, day = [ 1977, 10, 30 ]
# birth_in_seconds = Date.new(year,month,day).to_time.to_i
birth_in_seconds = Date.new(*birthdate).to_time.to_i
today_in_seconds = Date.today.to_time.to_i
age = today_in_seconds - birth_in_seconds
class Date
EARTH_YEAR_IN_SECONDS = 31557600
end
def planet_years(age_in_seconds,ratio_to_earth_years)
age_in_seconds / (Date::EARTH_YEAR_IN_SECONDS * ratio_to_earth_years)
end
def earth_years(age_in_seconds)
planet_years(age_in_seconds,1)
end
def mercury_years(age_in_seconds)
planet_years(age_in_seconds,0.2408467)
end
# ...
def neptune_years(age_in_seconds)
planet_years(age_in_seconds,164.79132)
end
puts earth_years(age)
puts mercury_years(age)
puts neptune_years(age)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment