Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 522afc7360a5
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 4fcdd09a3c62
Choose a head ref
  • 2 commits
  • 5 files changed
  • 2 contributors

Commits on Apr 13, 2015

  1. Copy the full SHA
    193c5d9 View commit details
  2. Merge pull request #795 from vais/float

    Kernel#Float fully compliant with rubyspec
    elia committed Apr 13, 2015
    Copy the full SHA
    4fcdd09 View commit details
Showing with 31 additions and 13 deletions.
  1. +22 −12 opal/corelib/kernel.rb
  2. +4 −1 opal/corelib/numeric.rb
  3. +3 −0 spec/filters/unsupported/float.rb
  4. +1 −0 spec/filters/unsupported/private_methods.rb
  5. +1 −0 spec/rubyspecs
34 changes: 22 additions & 12 deletions opal/corelib/kernel.rb
Original file line number Diff line number Diff line change
@@ -478,11 +478,7 @@ def Integer(value, base = undefined)
str = value.toLowerCase();
if (/^\s*_|__|_\s*$/.test(str)) {
#{raise ArgumentError, "invalid value for Integer(): \"#{value}\""}
}
str = str.replace(/_/g, '');
str = str.replace(/(\d)_(\d)/g, '$1$2');
if (!/^\s*[+-]?[0-9a-z]+\s*$/.test(str)) {
#{raise ArgumentError, "invalid value for Integer(): \"#{value}\""}
@@ -526,13 +522,27 @@ def Integer(value, base = undefined)
end

def Float(value)
if String === value
`parseFloat(value)`
elsif value.respond_to? :to_f
value.to_f
else
raise TypeError, "can't convert #{value.class} into Float"
end
%x{
var str;
if (value === nil) {
#{raise TypeError, "can't convert nil into Float"}
}
if (value.$$is_string) {
str = value.toString();
str = str.replace(/(\d)_(\d)/g, '$1$2');
if (!/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/.test(str)) {
#{raise ArgumentError, "invalid value for Float(): \"#{value}\""}
}
return parseFloat(str);
}
return #{Opal.coerce_to!(value, Float, :to_f)};
}
end

def is_a?(klass)
5 changes: 4 additions & 1 deletion opal/corelib/numeric.rb
Original file line number Diff line number Diff line change
@@ -285,7 +285,10 @@ def downto(finish, &block)
end

alias eql? ==
alias equal? ==

def equal?(other)
self == other || `isNaN(self) && isNaN(other)`
end

def even?
`self % 2 === 0`
3 changes: 3 additions & 0 deletions spec/filters/unsupported/float.rb
Original file line number Diff line number Diff line change
@@ -3,4 +3,7 @@
fails "Array#to_s represents a recursive element with '[...]'"
fails "Array#eql? returns false if any corresponding elements are not #eql?"
fails "Set#eql? returns true when the passed argument is a Set and contains the same elements"

fails "Kernel#Float raises a TypeError if #to_f returns an Integer"
fails "Kernel.Float raises a TypeError if #to_f returns an Integer"
end
1 change: 1 addition & 0 deletions spec/filters/unsupported/private_methods.rb
Original file line number Diff line number Diff line change
@@ -41,5 +41,6 @@
fails "Math#atanh is a private instance method"

fails "Kernel#Integer is a private method"
fails "Kernel#Float is a private method"
fails "Kernel#warn is a private method"
end
1 change: 1 addition & 0 deletions spec/rubyspecs
Original file line number Diff line number Diff line change
@@ -53,6 +53,7 @@ corelib/core/kernel/eql_spec
corelib/core/kernel/equal_spec
corelib/core/kernel/equal_value_spec
corelib/core/kernel/Integer_spec
corelib/core/kernel/Float_spec
corelib/core/kernel/tap_spec
corelib/core/kernel/to_s_spec
corelib/core/kernel/warn_spec