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

Commits on May 30, 2015

  1. Implement Array#to_h

    vais committed May 30, 2015
    Copy the full SHA
    f1a87ca View commit details
  2. Merge pull request #901 from vais/array-to_h

    Implement Array#to_h
    elia committed May 30, 2015
    Copy the full SHA
    4a43768 View commit details
Showing with 21 additions and 7 deletions.
  1. +21 −0 opal/corelib/array.rb
  2. +0 −7 spec/filters/bugs/array.rb
21 changes: 21 additions & 0 deletions opal/corelib/array.rb
Original file line number Diff line number Diff line change
@@ -1699,6 +1699,27 @@ def to_a

alias to_ary to_a

def to_h
%x{
var i, len = self.length, ary, key, val, hash = #{{}};
for (i = 0; i < len; i++) {
ary = #{Opal.coerce_to?(`self[i]`, Array, :to_ary)};
if (!ary.$$is_array) {
#{raise TypeError, "wrong element type #{`ary`.class} at #{`i`} (expected array)"}
}
if (ary.length !== 2) {
#{raise ArgumentError, "wrong array length at #{`i`} (expected 2, was #{`ary`.length})"}
}
key = ary[0];
val = ary[1];
hash.$store(key, val);
}
return hash;
}
end

alias to_s inspect

def transpose
7 changes: 0 additions & 7 deletions spec/filters/bugs/array.rb
Original file line number Diff line number Diff line change
@@ -154,11 +154,4 @@

fails "Array#first raises a RangeError when count is a Bignum"
fails "Array#hash calls to_int on result of calling hash on each element"
fails "Array#to_h converts empty array to empty hash"
fails "Array#to_h converts [key, value] pairs to a hash"
fails "Array#to_h uses the last value of a duplicated key"
fails "Array#to_h calls #to_ary on contents"
fails "Array#to_h raises TypeError if an element is not an array"
fails "Array#to_h raises ArgumentError if an element is not a [key, value] pair"
fails "Array#to_h does not accept arguments"
end