Skip to content

Instantly share code, notes, and snippets.

@zaeleus
Created March 26, 2012 22:27
Show Gist options
  • Save zaeleus/2210228 to your computer and use it in GitHub Desktop.
Save zaeleus/2210228 to your computer and use it in GitHub Desktop.
def self.coerce_to_float(obj, strict=false)
case obj
when Numeric
coerce_to obj, Float, :to_f
when Float
return obj
when String
result = Rubinius.invoke_primitive :string_to_f, obj, strict
raise ArgumentError, "invalid value for Float" if result.nil?
result
when nil, true, false
raise TypeError, "can't convert #{obj.inspect} into Float"
else
raise TypeError, "can't convert #{obj.class} into Float"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment