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: ae3ae6c32550
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: d37547747f5e
Choose a head ref
  • 2 commits
  • 1 file changed
  • 2 contributors

Commits on Apr 15, 2015

  1. Kernel#Float accepts hex strings as a special case

    This behavior is not specified in rubyspec for `Kernel#Float`, but it
    is tested in `Kernel#format`.
    
    Surprisingly, `Kernel#Float` accepts hexadecimal strings, but no other
    integer notation that is accepted by `Kernel#Integer`. I.e. `0x10` is
    ok, but `0b10`, `0d10`, `0o10` are not acceptable. Looks like a bug to
    me. I guess this PR makes Opal’s `Kernel#Float` a bug-for-bug
    implementation of MRI’s `Kernel#Float` :)
    
    ```
    $ pry
    [1] pry(main)> Float("010")
    => 10.0
    [2] pry(main)> Float("0o10")
    ArgumentError: invalid value for Float(): "0o10"
    from (pry):2:in `Float'
    [3] pry(main)> Float("0b10")
    ArgumentError: invalid value for Float(): "0b10"
    from (pry):3:in `Float'
    [4] pry(main)> Float("0d10")
    ArgumentError: invalid value for Float(): "0d10"
    from (pry):4:in `Float'
    [5] pry(main)> Float("0x10")
    => 16.0
    ```
    vais committed Apr 15, 2015
    Copy the full SHA
    10ae9bd View commit details
  2. Merge pull request #800 from vais/kernel-float-accepts-hex-strings

    Kernel#Float accepts hex strings as a special case
    meh committed Apr 15, 2015
    Copy the full SHA
    d375477 View commit details
Showing with 5 additions and 0 deletions.
  1. +5 −0 opal/corelib/kernel.rb
5 changes: 5 additions & 0 deletions opal/corelib/kernel.rb
Original file line number Diff line number Diff line change
@@ -538,6 +538,11 @@ def Float(value)
str = str.replace(/(\d)_(?=\d)/g, '$1');
//Special case for hex strings only:
if (/^\s*[-+]?0[xX][0-9a-fA-F]+\s*$/.test(str)) {
return #{Integer(`str`)};
}
if (!/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/.test(str)) {
#{raise ArgumentError, "invalid value for Float(): \"#{value}\""}
}