Skip to content

Commit

Permalink
More Kernel#Integer compliancy
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed Oct 17, 2013
1 parent c9138f4 commit e3c2c1e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions corelib/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class IndexError < StandardError; end
class StopIteration < IndexError; end
class KeyError < IndexError; end
class RangeError < StandardError; end
class FloatDomainError < RangeError; end
class IOError < StandardError; end

class ScriptError < Exception; end
Expand Down
38 changes: 32 additions & 6 deletions corelib/kernel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,41 @@ def instance_variables
}
end

def Integer(value, base = `undefined`)
def Integer(value, base = nil)
if String === value
`parseInt(value, base)`
elsif value.respond_to? :to_int
if value.empty?
raise ArgumentError, "invalid value for Integer: (empty string)"
end

return `parseInt(#{value}, #{base || `undefined`})`
end

if base
raise ArgumentError "base is only valid for String values"
end

case value
when Integer
value

when Float
if value.nan? or value.infinite?
raise FloatDomainError, "unable to coerce #{value} to Integer"
end

value.to_int
elsif value.respond_to? :to_i
value.to_i

when NilClass
raise TypeError, "can't convert nil into Integer"

else
raise TypeError, "can't convert #{value.class} into Integer"
if value.respond_to? :to_int
value.to_int
elsif value.respond_to? :to_i
value.to_i
else
raise TypeError, "can't convert #{value.class} into Integer"
end
end
end

Expand Down

2 comments on commit e3c2c1e

@adambeynon
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@meh even though I think the problems lie else where, Im thinking about moving these Integer() changes to a branch until we can fix the failing specs. I think we need to keep master passing.

@meh
Copy link
Member Author

@meh meh commented on e3c2c1e Oct 18, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As said on IRC, I'd rather have those specs marked as failing, I need Integer() now.

I looked into it and the issue is not where it seems, it's something completely unrelated to the values getting passed in.

Please sign in to comment.