Created
March 26, 2012 22:27
-
-
Save zaeleus/2210228 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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