Skip to content

Instantly share code, notes, and snippets.

@shaliko
Created August 10, 2010 05:47
Show Gist options
  • Save shaliko/516761 to your computer and use it in GitHub Desktop.
Save shaliko/516761 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
100000.downto(0) do |n|
number = rand(10) + n.to_f/100000
converted = MetricSystem::convert(:km, :mi, MetricSystem::convert(:mi, :km, number))
if number != number
puts "number = #{number} & convert = #{converted}"
else
#puts "number = #{number} & convert = #{converted}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment