Skip to content

Instantly share code, notes, and snippets.

@shaliko
Created August 9, 2010 14:33
Show Gist options
  • Save shaliko/515485 to your computer and use it in GitHub Desktop.
Save shaliko/515485 to your computer and use it in GitHub Desktop.
module MetricSystem
def self.convert(from, to, value)
raise ArgumentError if from.blank? || to.blank? || value.blank?
from = from.to_sym
to = to.to_sym
if from == :kg && to == :g
value.to_f * 1000
elsif from == :g && to == :kg
value.to_f / 1000
elsif from == :pound && to == :g
value.to_f * 453.59237
elsif from == :g && to == :pound
value.to_f / 453.59237
elsif from == :mi && to == :km
value.to_f * 1.60934
elsif from == :km && to == :mi
value.to_f / 1.60934
elsif from == :mi && to == :m
value.to_f * 1609.34
elsif from == :m && to == :mi
value.to_f / 1609.34
else
nil
end
end
end
puts MetricSystem::convert(:km, :mi, MetricSystem::convert(:mi, :km, 1.23456))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment