Skip to content

Instantly share code, notes, and snippets.

@kevinmpowell
Created December 16, 2015 01:20
Show Gist options
  • Save kevinmpowell/fe0613f9e22faf198fc0 to your computer and use it in GitHub Desktop.
Save kevinmpowell/fe0613f9e22faf198fc0 to your computer and use it in GitHub Desktop.
# Input strings like "52 lb 6 oz" or "8 oz"
def weight_string_to_float(weight_string)
/(?<lb>[0-9]*\slb\s)*(?<oz>[0-9]*\soz)/ =~ weight_string
lbs = 0
unless lb.nil?
lbs += lb.to_i
end
unless oz.nil?
lbs += (oz.to_i * 0.0625)
end
lbs
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment