Skip to content

Instantly share code, notes, and snippets.

@ZempTime
Last active August 29, 2015 14:22
Show Gist options
  • Save ZempTime/6c9701935b23d5c8cba1 to your computer and use it in GitHub Desktop.
Save ZempTime/6c9701935b23d5c8cba1 to your computer and use it in GitHub Desktop.
Weight Formatter
class WeightFormatter
PROCESSABLE_WEIGHT_UNITS = ["grams", "ounces"]
def self.call(weight_unit, weight)
unless PROCESSABLE_WEIGHT_TYPES.include?(weight_unit)
return "#{weight} #{weight_unit}"
end
send("process_#{weight_unit.downcase}", weight)
end
private
def self.process_grams(weight)
oz = (weight.to_i * 0.035274).round
lbs = oz / 16
ounces = oz % 16
"#{lbs} lbs. #{ounces} oz."
end
def self.process_ounces(weight)
lbs = weight.to_i.round / 16
ounces = weight.to_i.round % 16
"#{lbs} lbs. #{ounces} oz."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment