Skip to content

Instantly share code, notes, and snippets.

@arya
Created February 8, 2012 18:32
Show Gist options
  • Save arya/1771983 to your computer and use it in GitHub Desktop.
Save arya/1771983 to your computer and use it in GitHub Desktop.
class Module
UNITS = %w{second minute hour day week month year solar_year}.map { |s| s.upcase }
CONVERSIONS = [1, 60, 60, 24, 7, 30 / 7.0, 365 / 30.0, 31558150 / 31536000.0]
def const_missing(name)
if name.to_s =~ /^T_([0-9]+)_(#{UNITS.join("|")})S?$/
num = $1.to_i
unit = $2
previous_unit_index = UNITS.index(unit) - 1
value = if previous_unit_index < 0
num
else
(num * const_get("T_1_#{UNITS[previous_unit_index]}") * CONVERSIONS[UNITS.index(unit)]).to_i
end
const_set(name, value)
value
else
super
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment