Skip to content

Instantly share code, notes, and snippets.

@srabuini
Created August 15, 2015 03:50
Show Gist options
  • Save srabuini/9d28bb5be8c2e8f0a7b1 to your computer and use it in GitHub Desktop.
Save srabuini/9d28bb5be8c2e8f0a7b1 to your computer and use it in GitHub Desktop.
class WestheimerRule
UNITS = %i(seconds minutes hours days weeks months years lustra decades
centuries millennniums)
def initialize(time:, unit:)
@time = time
@unit = unit
end
def estimate
"#{2 * @time} #{next_unit}"
end
private
def next_unit
UNITS[next_unit_index]
end
def next_unit_index
unit_index + 1
end
def unit_index
UNITS.index(@unit)
end
end
WestheimerRule.new(time: 2, unit: :weeks).estimate
# => "4 months"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment