Skip to content

Commit

Permalink
Fixing Kernel.Float, all specs passing.
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarsa committed Aug 7, 2009
1 parent a71cf3c commit 270c5bf
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
2 changes: 2 additions & 0 deletions kernel/bootstrap/string.rb
Expand Up @@ -9,6 +9,8 @@ def to_f
raise PrimitiveFailure, "String#to_f primitive failed"
end

alias_method :convert_float, :to_f

def __crypt__(other_str)
Ruby.primitive :string_crypt
raise PrimitiveFailure, "String#crypt primitive failed"
Expand Down
11 changes: 9 additions & 2 deletions kernel/common/kernel.rb
Expand Up @@ -3,13 +3,20 @@ module Kernel
def Float(obj)
raise TypeError, "can't convert nil into Float" if obj.nil?

if obj.is_a?(String)
if obj.is_a?(Float)
return obj
elsif obj.is_a?(String)
if obj !~ /^\s*[+-]?((\d+_?)*\d+(\.(\d+_?)*\d+)?|\.(\d+_?)*\d+)(\s*|([eE][+-]?(\d+_?)*\d+)\s*)$/
raise ArgumentError, "invalid value for Float(): #{obj.inspect}"
end
return obj.convert_float
end

Type.coerce_to(obj, Float, :to_f)
coerced_value = Type.coerce_to(obj, Float, :to_f)
if coerced_value.nan?
raise ArgumentError, "invalid value for Float(): #{coerced_value.inspect}"
end
coerced_value
end
module_function :Float

Expand Down
6 changes: 0 additions & 6 deletions spec/tags/frozen/core/kernel/Float_tags.txt
@@ -1,7 +1 @@
incomplete:Kernel#Float needs to be reviewed for spec completeness
fails:Kernel.Float converts Strings to floats without calling #to_f
fails:Kernel.Float converts String subclasses to floats without calling #to_f
fails:Kernel.Float raises an Argument Error if to_f is called and it returns NaN
fails:Kernel#Float converts Strings to floats without calling #to_f
fails:Kernel#Float converts String subclasses to floats without calling #to_f
fails:Kernel#Float raises an Argument Error if to_f is called and it returns NaN

0 comments on commit 270c5bf

Please sign in to comment.