Skip to content

Commit

Permalink
Enable Array#[]= to accept a range (with fixed specs)
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Sep 20, 2013
1 parent 2651a89 commit 9067f70
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
20 changes: 20 additions & 0 deletions corelib/array.rb
Expand Up @@ -288,6 +288,26 @@ def []=(index, value, extra = undefined)
%x{
var size = #{self}.length;
if (typeof index !== 'number' && !index._isNumber) {
if (index._isRange) {
var exclude = index.exclude;
extra = value;
value = index.end;
index = index.begin;
if (value < 0) {
value += size;
}
if (!exclude) value += 1;
value = value - index;
}
else {
#{raise ArgumentError};
}
}
if (index < 0) {
index += size;
}
Expand Down
10 changes: 1 addition & 9 deletions spec/filters/bugs/array/element_set.rb
Expand Up @@ -8,18 +8,10 @@
fails "Array#[]= with [m..n] inserts the other section at m if m > n"
fails "Array#[]= with [m..n] replaces the section if m < 0 and n > 0"
fails "Array#[]= with [m..n] replaces the section if m and n < 0"
fails "Array#[]= with [m..n] replaces the section defined by range"
fails "Array#[]= with [m..n] just sets the section defined by range to nil if m and n < 0 and the rhs is nil"
fails "Array#[]= with [m..n] just sets the section defined by range to nil even if the rhs is nil"

fails "Array#[]= just inserts nil if the section defined by range has negative width and the rhs is nil"
fails "Array#[]= just inserts nil if the section defined by range is zero-width and the rhs is nil"
fails "Array#[]= inserts the given elements with [range] which the range has negative width"
fails "Array#[]= inserts the given elements with [range] which the range is zero-width"
fails "Array#[]= sets elements in the range arguments when passed ranges"
fails "Array#[]= checks frozen before attempting to coerce arguments"
fails "Array#[]= calls to_int on its start and length arguments"
fails "Array#[]= just sets the section defined by range to other even if other is nil"
fails "Array#[]= replaces the section defined by range with the given values"
fails "Array#[]= sets the section defined by range to other"
fails "Array#[]= does nothing if the section defined by range has negative width and the rhs is an empty array"
end

0 comments on commit 9067f70

Please sign in to comment.