Class: Array

Inherits:
Object show all
Defined in:
lib/source/ruby.rb

Overview

Arrays are ordered, integer-indexed collections of any object. Array indexing starts at 0. A negative index is assumed to be relative to the end of the array – that is, an index of -1 indicates the last element of the array, -2 is the next to last element in the array, and so on.

Instance Method Summary collapse

Instance Method Details

#&(ary) ⇒ Object

call-seq:

ary & other -> array

Set Intersection – returns a new array containing elements common to ary and other, with no duplicates.

[1,1,3,5] & [1,2,3]   #=> [1, 3]


1741
1742
1743
1744
# File 'lib/source/ruby.rb', line 1741

def &(ary)
  `for(var i=0,l=this.length,result=[],found=false,seen={};i<l;++i){var a=this[i],k=a.m$hash();for(var j=0,m=ary.length;j<m;++j){var b=this[j];if(a.m$_eql2(b)){found=true;break;};};if(found&&!seen[k]){seen[k]=true;result.push(a);found=false;};}`
  return `result`
end

#*(arg) ⇒ Object

call-seq:

ary * num -> array
ary * str -> string

Repetition – with a String argument, equivalent to self.join(str). Otherwise, returns a new array built by concatenating num copies of self.

[1,2,3] * ':'   #=> "1:2:3"
[1,2,3] * 3     #=> [1, 2, 3, 1, 2, 3, 1, 2, 3]


1771
1772
1773
1774
1775
1776
# File 'lib/source/ruby.rb', line 1771

def *(arg)
  `if(arg.m$class()==c$String){return $q(this.join(arg));}`
  `var result=[],i=0,l=parseInt(arg)`
  `while(i<l){result=result.concat(this);i++;}`
  return `result`
end

#+(ary) ⇒ Object

call-seq:

ary + other -> array

Concatenation – returns a new array built by concatenating ary and other.

[1,2,3] + [4,5]   #=> [1, 2, 3, 4, 5]


1786
1787
1788
# File 'lib/source/ruby.rb', line 1786

def +(ary)
  `this.concat(ary)`
end

#-(ary) ⇒ Object

call-seq:

ary - other -> array

Difference – returns a new array containing only items that appear in ary and not in other.

[1,1,2,2,3,3,4,5] - [1,2,4]   #=> [3, 3, 5]


1798
1799
1800
1801
1802
# File 'lib/source/ruby.rb', line 1798

def -(ary)
  `for(var i=0,l=ary.length,result=[],seen=[];i<l;++i){var a=ary[i],k=a.m$hash();if(!seen[k]){seen[k]=true;};};`
  `for(var i=0,l=this.length;i<l;++i){var a=this[i],k=a.m$hash();if(!seen[k]){result.push(a);};}`
  return `result`
end

#<<(object) ⇒ Object

call-seq:

ary << obj -> ary

Append – pushes the given object onto the end of ary. This expression returns ary itself, so several appends may be chained together.

[1,2] << 'c' << 'd' << [3,4]    #=> [1, 2, "c", "d", [3, 4]]


1812
1813
1814
1815
# File 'lib/source/ruby.rb', line 1812

def <<(object)
  `this[this.length]=object`
  return self
end

#<=>(ary) ⇒ Object

call-seq:

ary <=> other -> -1, 0, 1

Comparison – returns -1, 0, or 1 depending on whether ary is less than, equal to, or greater than other. Each object in both arrays is compared using <=>; if any comparison fails to return 0, Array#<=> returns the result of that comparison. If all the values found are equal, returns the result of comparing the lengths of ary and other. Thus, ary <=> other returns 0 if and only if both arrays are the same length and the value of each element is equal to the value of the corresponding element in the other array.

%w(a a c)     <=> %w(a b c)   #=> -1
[1,2,3,4,5,6] <=> [1,2]       #=> 1


1832
1833
1834
1835
# File 'lib/source/ruby.rb', line 1832

def <=>(ary)
  `for(var i=0,l=this.length;i<l;++i){if(ary[i]==null){break;};var x=this[i].m$_ltgt(ary[i]);if(x!==0){return x;};}`
  return `this.length.m$_ltgt(ary.length)`
end

#==(ary) ⇒ Object

call-seq:

ary == other -> true or false

Equality – returns true if ary and other contain the same number of elements and if for every index i in ary, ary[i] == other[i].

['a','c']    == ['a', 'c', 7]     #=> false
['a','c', 7] == ['a', 'c', 7]     #=> true
['a','c', 7] == ['a', 'd', 'f']   #=> false


1848
1849
1850
1851
1852
# File 'lib/source/ruby.rb', line 1848

def ==(ary)
  `if(ary.m$class()!==c$Array||ary.length!==this.length){return false;}`
  `for(var i=0,l=this.length;i<l;++i){if(!(this[i].m$_eql2(ary[i]))){return false;};}`
  return true
end

#[](index, length = nil) ⇒ Object

call-seq:

ary[index]               -> object or nil
ary[start, length]       -> array or nil
ary[range]               -> array or nil
ary.slice(index)         -> object or nil
ary.slice(start, length) -> array or nil
ary.slice(range)         -> array or nil

Element Reference – returns the element at index, or an array of the elements in range or from start to start plus length. Returns nil if the index is out of range.

a = %w(a b c d e)

a[2] + a[0] + a[1]    #=> "cab"
a[6]                  #=> nil
a[1,2]                #=> ["b", "c"]
a[1..3]               #=> ["b", "c", "d"]
a[4..7]               #=> ["e"]
a[6..10]              #=> nil
a[-3,3]               #=> ["c", "d", "e"]
# special cases
a[5]                  #=> nil
a[5,1]                #=> []
a[5..10]              #=> []


1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
# File 'lib/source/ruby.rb', line 1880

def [](index, length = nil)
  `var l=this.length`
  `if(index.m$class()==c$Range){
    var start=index.__start__,end=index.__exclusive__?index.__end__-1:index.__end__;
    index=start<0?start+l:start;
    length=(end<0?end+l:end)-index+1;
    if(length<0){length=0};
  }else{
    if(index<0){index+=l;};
  }`
  `if(index>=l||index<0){return nil;}`
  `if($T(length)){
    if(length<=0){return [];};
    result=this.slice(index,index+length);
  }else{
    result=this[index];
  }`
  return `result`
end

#[]=(index, length, object) ⇒ Object

call-seq:

ary[index] = obj                         -> obj
ary[start, length] = obj or array or nil -> obj or array or nil
ary[range] = obj or array or nil         -> obj or array or nil

Element Assignment – sets the element at _index, or replaces the elements in range or from start to start plus length, truncating or expanding ary as necessary. If nil is used in the second and third form, deletes elements. See also Array#push, and Array#unshift.

a = []

a[4]     = '4'         #=> [nil, nil, nil, nil, "4"]
a[0,3]   = %w(a b c)   #=> ["a", "b", "c", nil, "4"]
a[1..2]  = [1,2]       #=> ["a", 1, 2, nil, "4"]
a[0,2]   = '?'         #=> ["?", 2, nil, "4"]
a[0..2]  = 'A'         #=> ["A", "4"]
a[-1]    = 'Z'         #=> ["A", "Z"]
a[1..-1] = nil         #=> ["A"]


1921
1922
1923
1924
1925
1926
1927
1928
1929
# File 'lib/source/ruby.rb', line 1921

def []=(index, length, object)
  `var l=this.length`
  `if(object==null){object=length;length=$u;}`
  `if(index.m$class()==c$Range){var start=index.__start__,end=index.__exclusive__?index.__end__-1:index.__end__;index=start<0?start+l:start;length=(end<0?end+l:end)-index+1;if(length<0){length=0};}else{if(index<0){index+=l;};if(length<0){throw('IndexError: negative length')}}`
  `if(index<0){throw('RangeError: out of range');}`
  `while(this.length<index){this.push(nil);}`
  `if($T(length)){var l=this.length,final=(index+length>l)?l:index+length;this._replace(this.slice(0,index).concat(object===nil?[]:(object.m$class()==c$Array?object:[object])).concat(this.slice(final,l)))}else{this[index]=object}`
  return `object`
end

#__id__Object

:nodoc:



1931
1932
1933
# File 'lib/source/ruby.rb', line 1931

def __id__ # :nodoc:
  `this.__id__||(this.__id__=Red.id++)`
end

#assoc(obj) ⇒ Object

call-seq:

ary.assoc(obj) -> array or nil

Searches through ary, comparing obj with the first element of each ary element that is also an array. Returns the first array such that array[0] == obj, or nil if no match is found. See also Array#rassoc.

a  = [%w(colors red blue green), %w(letters a b c), 'foo']

a.assoc('letters')    #=> ["letters", "a", "b", "c"]
a.assoc('foo')        #=> nil


1948
1949
1950
1951
# File 'lib/source/ruby.rb', line 1948

def assoc(obj)
  `for(var i=0,l=this.length;i<l;++i){var x=this[i];if(x.m$class()==c$Array&&x[0]!=null&&x[0].m$_eql2(obj)){return x;}}`
  return nil
end

#at(index) ⇒ Object

call-seq:

ary.at(index) -> object or nil

Returns the element at index. A negative index counts from the end of ary. Returns nil if index is out of range. See also Array#[]. (Array#at is slightly faster than Array#[], because it does not accept ranges and so on.)

%w(a b c d e).at(0)     #=> "a"
%w(a b c d e).at(-1)    #=> "e"


1964
1965
1966
1967
1968
# File 'lib/source/ruby.rb', line 1964

def at(index)
  `if(index<0){index+=this.length;}`
  `if(index<0||index>=this.length){return nil;}`
  return `this[index]`
end

#clearObject

call-seq:

ary.clear -> ary

Removes all elements from ary.

a = %w(a b c d e)

a.clear     #=> []
a           #=> []


1980
1981
1982
1983
# File 'lib/source/ruby.rb', line 1980

def clear
  `this.length=0`
  return self
end

#collectObject

call-seq:

ary.collect { |element| block } -> array
ary.map     { |element| block } -> array

Calls block once for each element in ary, then returns a new array containing the values returned by the block. See also Enumerable#collect.

a = [1,2,3,4]

a.collect {|x| x + 100 }    #=> [101, 102, 103, 104]
a                           #=> [1, 2, 3, 4]


1998
1999
2000
2001
# File 'lib/source/ruby.rb', line 1998

def collect
  `for(var i=0,l=this.length,result=[];i<l;++i){try{result[i]=#{yield `this[i]`};}catch(e){switch(e.__keyword__){case 'next':result[i]=e.__return__;break;case 'break':return e.__return__;break;case 'redo':--i;break;default:throw(e);};};}`
  return `result`
end

#collect!Object

call-seq:

ary.collect! { |element| block } -> ary
ary.map!     { |element| block } -> ary

Calls block once for each element in ary, replacing the element with the value returned by the block, then returns ary. See also Enumerable#collect.

a = [1,2,3,4]

a.collect! {|x| x + 100 }   #=> [101, 102, 103, 104]
a.collect! {|x| x + 100 }   #=> [201, 202, 203, 204]


2016
2017
2018
2019
# File 'lib/source/ruby.rb', line 2016

def collect!
  `for(var i=0,l=this.length;i<l;++i){try{this[i]=#{yield `this[i]`};}catch(e){switch(e.__keyword__){case 'next':this[i]=e.__return__;break;case 'break':return e.__return__;break;case 'redo':--i;break;default:throw(e);};};}`
  return self
end

#compactObject

call-seq:

ary.compact -> array

Returns a copy of ary with all nil elements removed.

['a',nil,'b',nil,'c',nil].compact   #=> ["a", "b", "c"]


2028
2029
2030
2031
# File 'lib/source/ruby.rb', line 2028

def compact
  `for(var i=0,l=this.length,result=[];i<l;++i){if(!(this[i]===nil)){result.push(this[i]);};}`
  return `result`
end

#compact!Object

call-seq:

ary.compact! -> ary or nil

Removes nil elements and returns ary, or nil if no changes were made.

a = ['a',nil,'b',nil,'c']

a.compact!    #=> ["a", "b", "c"]
a.compact!    #=> nil


2044
2045
2046
2047
2048
# File 'lib/source/ruby.rb', line 2044

def compact!
  `for(var i=0,l=this.length,temp=[];i<l;++i){if(!(this[i]===nil)){temp.push(this[i]);};}`
  `this._replace(temp)`
  return `l===this.length?nil:this`
end

#concat(ary) ⇒ Object

call-seq:

ary.concat(other) -> ary

Appends the elements in other to ary and returns ary.

[1,2].concat([3,4]).concat([5,6])   #=> [1, 2, 3, 4, 5, 6]


2057
2058
2059
2060
# File 'lib/source/ruby.rb', line 2057

def concat(ary)
  `for(var i=0,l=ary.length;i<l;++i){this.push(ary[i]);}`
  return self
end

#delete(obj) ⇒ Object

call-seq:

ary.delete(obj)           -> obj or nil
ary.delete(obj) { block } -> obj or block.call

Deletes items from ary that are equal to obj. If one or more objects are deleted from ary, returns obj; otherwise returns nil or the result of the optional code block.

a = %w(a b b b c)

a.delete('b')               #=> "b"
a                           #=> ["a", "c"]
a.delete('z')               #=> nil
a.delete('z') { 'FAIL' }    #=> "FAIL"


2077
2078
2079
2080
2081
# File 'lib/source/ruby.rb', line 2077

def delete(obj)
  `for(var i=0,l=this.length,temp=[];i<l;++i){if(!(this[i].m$_eql2(obj))){temp.push(this[i]);};}`
  `this._replace(temp)`
  return `l===this.length?(#{block_given?}?#{yield}:nil):obj`
end

#delete_at(index) ⇒ Object

call-seq:

ary.delete_at(index) -> object or nil

Deletes the element at the specified index, returning that element or nil if the index is out of range. See also Array#slice!.

a = %w(a b c d)

a.delete_at(2)    #=> "c"
a                 #=> ["a", "b", "d"]
a.delete_at(99)   #=> nil


2095
2096
2097
2098
2099
2100
2101
2102
2103
# File 'lib/source/ruby.rb', line 2095

def delete_at(index)
  `var l=this.length`
  `if(index<0){index+=this.length;}`
  `if(index<0||index>=this.length){return nil;}`
  `var result=this[index],temp=[]`
  `for(var i=0;i<l;++i){if(i!==index){temp.push(this[i]);};}`
  `this._replace(temp)`
  return `result`
end

#delete_ifObject

call-seq:

ary.delete_if { |element| block } -> ary

Deletes every element in ary for which block evaluates to true, then returns ary.

a = %w(a b c)

a.delete_if {|element| element >= 'b' }   #=> ["a"]


2115
2116
2117
2118
# File 'lib/source/ruby.rb', line 2115

def delete_if
  `for(var temp=[],i=0,l=this.length;i<l;++i){try{if(!$T(#{yield `this[i]`})){temp.push(this[i]);};}catch(e){switch(e.__keyword__){case 'next':if(!$T(e.__return__)){temp.push(this[i]);};break;case 'break':return e.__return__;break;case 'redo':--i;break;default:throw(e);};};}`
  `this._replace(temp)`
end

#eachObject

call-seq:

ary.each { |element| block } -> ary

Calls block once for each element in ary, then returns ary.

%(a b c).each {|element| puts element.upcase }    #=> ["a", "b", "c"]

produces:

A
B
C


2133
2134
2135
2136
# File 'lib/source/ruby.rb', line 2133

def each
  `for(var i=0,l=this.length;i<l;++i){try{#{yield `this[i]`};}catch(e){switch(e.__keyword__){case 'next':break;case 'break':return e.__return__;break;case 'redo':--i;break;default:throw(e);};};}`
  return self
end

#each_indexObject

call-seq:

ary.each_index { |index| block } -> ary

Calls block once for each index in ary, then returns ary.

%(a b c).each {|index| puts index + 100 }   #=> ["a", "b", "c"]

produces:

100
101
102


2151
2152
2153
2154
# File 'lib/source/ruby.rb', line 2151

def each_index
  `for(var i=0,l=this.length;i<l;++i){try{#{yield `i`};}catch(e){switch(e.__keyword__){case 'next':break;case 'break':return e.__return__;break;case 'redo':--i;break;default:throw(e);};};}`
  return self
end

#empty?Boolean

call-seq:

ary.empty? -> true or false

Returns true if ary contains no elements.

[''].empty?   #=> false
[].empty?     #=> true

Returns:

  • (Boolean)


2164
2165
2166
# File 'lib/source/ruby.rb', line 2164

def empty?
  `this.length==0`
end

#eql?(ary) ⇒ Boolean

call-seq:

ary.eql?(other) -> true or false

Equality – returns true if ary and other contain the same number of elements and if for every index i in ary, ary[i].eql?( other[i] ).

['a','c'].eql?    ['a','c', 7]    #=> false
['a','c', 7].eql? ['a','c', 7]    #=> true
['a','c', 7].eql? ['a','d','f']   #=> false

Returns:

  • (Boolean)


2179
2180
2181
2182
2183
# File 'lib/source/ruby.rb', line 2179

def eql?(ary)
  `if(ary.m$class()!==c$Array||ary.length!==this.length){return false;}`
  `for(var i=0,l=this.length;i<l;++i){if(!(this[i].m$eql_bool(ary[i]))){return false;};}`
  return true
end

#equal?(ary) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


2185
2186
2187
2188
# File 'lib/source/ruby.rb', line 2185

def equal?(ary) # :nodoc:
  `var thisId=this.__id__||(this.__id__=Red.id++),aryId=ary.__id__||(ary.__id__=Red.id++)`
  `thisId==aryId`
end

#fetch(index, &block) ⇒ Object

call-seq:

ary.fetch(index)                   -> object or nil
ary.fetch(index, default)          -> object or default
ary.fetch(index) { |index| block } -> object or block.call(index)

Tries to return the element at position index. Negative values of index count from the end of the array.

a = [100,101,102,103]

a.fetch(1)                        #=> 101
a.fetch(-1)                       #=> 103
a.fetch(5, 'FAIL')                #=> "FAIL"
a.fetch(5) {|i| "FAIL: #{i}" }    #=> "FAIL: 5"


2205
2206
2207
2208
2209
2210
# File 'lib/source/ruby.rb', line 2205

def fetch(index, &block)
  `var i=index`
  `if(index<0){index+=this.length;}`
  `if(index<0||index>=this.length){return typeof(block)=='function'?#{yield(`i`)}:block||nil;}`
  return `this[index]`
end

#fill(object, index = 0, length = nil) ⇒ Object

call-seq:

ary.fill(obj)                                 -> ary
ary.fill(obj, start [, length])               -> ary
ary.fill(obj, range )                         -> ary
ary.fill { |index| block }                    -> ary
ary.fill(start [, length] ) { |index| block } -> ary
ary.fill(range) { |index| block }             -> ary

The first three forms set the selected elements of ary (which may be the entire array) to obj. A start of nil is equivalent to zero. A length of nil is equivalent to ary.length. The last three forms fill ary with the value of the block. The block is passed the absolute index of each element to be filled.

a = %w(a b c d)

a.fill("x")              #=> ["x", "x", "x", "x"]
a.fill("z", 2, 2)        #=> ["x", "x", "z", "z"]
a.fill("y", 0..1)        #=> ["y", "y", "z", "z"]
a.fill {|i| i*i}         #=> [0, 1, 4, 9]
a.fill(-2) {|i| i*i*i}   #=> [0, 1, 8, 27]

FIX: Incomplete -> doesn’t accept ranges or handle loop control keywords



2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
# File 'lib/source/ruby.rb', line 2235

def fill(object, index = 0, length = nil)
  `if(!(typeof(object)=='function'||typeof(index)=='function'||typeof(length)=='function')){if(index<0){index+=this.length;};for(var i=index,final=($T(length)?index+length:this.length);i<final;++i){this[i]=object;};return(this);}`
  `var final=this.length,_block=$u`
  `if(typeof(object)=='function'){_block=object;}`
  `if(typeof(index)=='function'){_block=index;index=object;}`
  `if(typeof(length)=='function'){_block=length;length=index;index=object;if(index<0){index+=this.length;if(index<0){throw('IndexError: out of range')}};final=index+length;}`
  `if(index<0){index+=this.length;}`
  `for(var i=index;i<final;++i){this[i]=_block(i);}`
  return self
end

#first(n) ⇒ Object

call-seq:

ary.first    -> object or nil
ary.first(n) -> array

Returns the first element of ary, or an array of the first n elements of ary. If ary is empty, the first form returns nil and the second form returns an empty array.

a = %w(a b c d)

a.first       #=> "a"
a.first(1)    #=> ["a"]
a.first(3)    #=> ["a", "b", "c"]


2260
2261
2262
2263
# File 'lib/source/ruby.rb', line 2260

def first(n)
  `if(n!=null){for(var i=0,l=this.length,result=[],max=l<n?l:n;i<max;++i){result.push(this[i]);};return result;}`
  return `this.length==0?nil:this[0]`
end

#flattenObject

call-seq:

ary.flatten -> array

Returns a new array that is a one-dimensional flattening of ary, by extracting the elements of each ary element that is also an array into the new array.

a = [1,2,3]         #=> [1, 2, 3]
b = [4,5,6,[7,8]]   #=> [4, 5, 6, [7, 8]]
c = [a,b,9,10]      #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]

c.flatten           #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


2278
2279
2280
2281
# File 'lib/source/ruby.rb', line 2278

def flatten
  `for(var i=0,l=this.length,result=[];i<l;++i){if(this[i].m$class()==c$Array){result=result.concat(this[i].m$flatten());}else{result.push(this[i]);};}`
  return `result`
end

#flatten!Object

call-seq:

ary.flatten! -> ary or nil

Extracts the elements of each element that is also an array and returns ary, or nil if no changes were made.

a = [1,2,[3,[4,5]]]

a.flatten!   #=> [1, 2, 3, 4, 5]
a.flatten!   #=> nil


2294
2295
2296
2297
2298
# File 'lib/source/ruby.rb', line 2294

def flatten!
  `var flatten=function(ary){for(var i=0,l=ary.length,result=[];i<l;++i){if(ary[i].m$class()==c$Array){result=result.concat(ary[i].m$flatten());}else{result.push(ary[i]);};};return result;}`
  `for(var i=0,l=this.length,result=[];i<l;++i){if(this[i].m$class()==c$Array){result=result.concat(flatten(this[i]));}else{result.push(this[i]);};}`
  return `l==result.length?nil:this._replace(result)`
end

#hashObject

:nodoc:



2300
2301
# File 'lib/source/ruby.rb', line 2300

def hash # :nodoc:
end

#include?(obj) ⇒ Boolean

call-seq:

ary.include?(obj) -> true or false

Returns true if any object in ary == obj, false otherwise.

a = %w(a b c)

a.include?('b')   #=> true
a.include?('z')   #=> false

Returns:

  • (Boolean)


2314
2315
2316
2317
# File 'lib/source/ruby.rb', line 2314

def include?(obj)
  `for(var i=0,l=this.length;i<l;++i){if(this[i].m$_eql2(obj)){return true;};}`
  return false
end

#index(obj) ⇒ Object

call-seq:

ary.index(obj) -> integer or nil

Returns the index of the first object in ary that == obj, or nil if no match is found.

a = %w(a b c)

a.index('b')    #=> 1
a.index('z')    #=> nil


2330
2331
2332
2333
# File 'lib/source/ruby.rb', line 2330

def index(obj)
  `for(var i=0,l=this.length;i<l;++i){if(this[i].m$_eql2(obj)){return i;};}`
  return nil
end

#insert(index, *args) ⇒ Object

call-seq:

ary.insert(index, obj...) -> ary

Inserts the given values before the element at index and returns ary.

a = [1,2,3,4]

a.insert(2, 99)             #=> [1, 2, 99, 3, 4]
a.insert(-2,'a','b','c')    #=> [1, 2, 99, 3, "a", "b", "c", 4]


2345
2346
2347
2348
2349
# File 'lib/source/ruby.rb', line 2345

def insert(index, *args)
  `if(index<0){index+=this.length;if(index<0){throw('IndexError: out of range');};}`
  `while(this.length<index){this.push(nil);}`
  `this._replace(this.slice(0,index).concat(args).concat(this.slice(index,this.length)))`
end

#inspectObject

call-seq:

ary.inspect -> string

Returns a printable version of ary created by calling inspect on each element.

[1,2,3].inspect   #=> "[1, 2, 3]"


2359
2360
2361
2362
# File 'lib/source/ruby.rb', line 2359

def inspect
  `for(var i=1,l=this.length,result='['+(this[0]!=null?this[0].m$inspect().__value__:'');i<l;++i){result+=', '+this[i].m$inspect().__value__;}`
  return `$q(result+']')`
end

#join(str = '') ⇒ Object

call-seq:

ary.join(str = '') -> string

Returns a string version of ary created by calling to_s on each element (or join(str) if the element is also an array), then concatenating the resulting strings with str between them.

%w(a b c).join        #=> "abc"
%w(a b c).join('.')   #=> "a.b.c"


2374
2375
2376
2377
2378
# File 'lib/source/ruby.rb', line 2374

def join(str = '')
  `var result=this[0]!=null?this[0].m$join?this[0].m$join(str.__value__).__value__:this[0].m$to_s().__value__:''`
  `for(var i=1,l=this.length;i<l;++i){result+=(str.__value__||'')+(this[i].m$join?this[i].m$join(str).__value__:this[i].m$to_s().__value__);}`
  return `$q(result)`
end

#last(n) ⇒ Object

call-seq:

ary.last    -> object or nil
ary.last(n) -> array

Returns the last element of ary, or an array of the last n elements of ary. If ary is empty, the first form returns nil and the second form returns an empty array.

a = %w(a b c d)

a.last      #=> "d"
a.last(1)   #=> ["d"]
a.last(3)   #=> ["b", "c", "d"]


2394
2395
2396
2397
2398
# File 'lib/source/ruby.rb', line 2394

def last(n)
  `var l=this.length`
  `if(n!=null){for(var result=[],i=n>l?0:l-n;i<l;++i){result.push(this[i]);};return result;}`
  return `l==0?nil:this[l-1]`
end

#lengthObject

call-seq:

ary.length -> integer
ary.size   -> integer

Returns the number of elements in ary. May be zero.

%w(a b c d e).length    #=> 5


2408
2409
2410
# File 'lib/source/ruby.rb', line 2408

def length
  `this.length`
end

#mapObject

call-seq:

ary.collect { |element| block } -> array
ary.map     { |element| block } -> array

Calls block once for each element in ary, then returns a new array containing the values returned by the block. See also Enumerable#collect.

a = [1,2,3,4]

a.map {|x| x + 100 }    #=> [101, 102, 103, 104]
a                       #=> [1, 2, 3, 4]


2425
2426
2427
2428
# File 'lib/source/ruby.rb', line 2425

def map
  `for(var i=0,l=this.length,result=[];i<l;++i){try{result[i]=#{yield `this[i]`};}catch(e){switch(e.__keyword__){case 'next':result[i]=e.__return__;break;case 'break':return e.__return__;break;case 'redo':--i;break;default:throw(e);};};}`
  return `result`
end

#map!Object

call-seq:

ary.collect! { |element| block } -> ary
ary.map!     { |element| block } -> ary

Calls block once for each element in ary, replacing the element with the value returned by the block, then returns ary. See also Enumerable#collect.

a = [1,2,3,4]

a.map! {|x| x + 100 }   #=> [101, 102, 103, 104]
a.map! {|x| x + 100 }   #=> [201, 202, 203, 204]


2443
2444
2445
2446
# File 'lib/source/ruby.rb', line 2443

def map!
  `for(var i=0,l=this.length;i<l;++i){try{this[i]=#{yield `this[i]`};}catch(e){switch(e.__keyword__){case 'next':this[i]=e.__return__;break;case 'break':return e.__return__;break;case 'redo':--i;break;default:throw(e);};};}`
  return self
end

#nitemsObject

call-seq:

ary.nitems -> integer

Returns the number of non-nil elements in ary. May be zero.

[1, nil, 3, nil, 5].nitems    #=> 3


2455
2456
2457
2458
# File 'lib/source/ruby.rb', line 2455

def nitems
  `for(var i=0,l=this.length,result=0;i<l;++i){if(this[i]!==nil){result++;};}`
  return `result`
end

#object_idObject

:nodoc:



2460
2461
2462
# File 'lib/source/ruby.rb', line 2460

def object_id # :nodoc:
  `this.__id__||(this.__id__=Red.id++)`
end

#popObject

call-seq:

ary.pop -> object or nil

Removes the last element from ary and returns it, or nil if ary is empty.

a = %w(a b c)

a.pop   #=> "c"
a.pop   #=> "b"
a       #=> ["a"]


2476
2477
2478
2479
# File 'lib/source/ruby.rb', line 2476

def pop
  `if(this.length==0){return nil;}`
  `this.pop()`
end

#push(*args) ⇒ Object

call-seq:

ary.push(obj, ...) -> ary

Append – pushes the given objects onto the end of ary. This expression returns ary itself, so several appends may be chained together.

a = [1,2,3]

a.push(4).push(5,6,7)   #=> [1, 2, 3, 4, 5, 6, 7]


2491
2492
2493
2494
# File 'lib/source/ruby.rb', line 2491

def push(*args)
  `for(var i=0,l=args.length;i<l;++i){this.push(args[i]);}`
  return self
end

#rassoc(obj) ⇒ Object

call-seq:

ary.rassoc(obj) -> array or nil

Searches through ary, comparing obj with the second element of each ary element that is also an array. Returns the first array such that array[1] == obj, or nil if no match is found. See also Array#assoc.

a  = [[1,'one'], [2,'two'], [:ii,'two']]

a.rassoc('two')     #=> [2, "two"]
a.rassoc('three')   #=> nil


2509
2510
2511
2512
# File 'lib/source/ruby.rb', line 2509

def rassoc(obj)
  `for(var i=0,l=this.length;i<l;++i){var x=this[i];if(x.m$class()==c$Array&&x[1]!=null&&x[1].m$_eql2(obj)){return x;};}`
  return nil
end

#rejectObject

call-seq:

ary.reject { |element| block } -> array

Returns a new array containing only the items in ary for which block.call(element) evaluates to nil or false.

a = [1,2,3,4,5]

a.reject {|x| x > 3 }   #=> [1, 2, 3]
a                       #=> [1, 2, 3, 4, 5]


2525
2526
2527
2528
# File 'lib/source/ruby.rb', line 2525

def reject
  `for(var i=0,l=this.length,result=[];i<l;++i){try{if(!$T(#{yield `this[i]`})){result.push(this[i]);};}catch(e){switch(e.__keyword__){case 'next':if(!$T(e.__return__)){result.push(this[i]);};break;case 'break':return e.__return__;break;case 'redo':--i;break;default:throw(e);};};}`
  return `result`
end

#reject!Object

call-seq:

ary.reject! { |element| block } -> ary or nil

Deletes every element in ary for which block evaluates to true, then returns ary, or nil if no changes were made.

a = [1,2,3,4,5]

a.reject! {|x| x > 3 }    #=> [1, 2, 3]
a.reject! {|x| x > 3 }    #=> nil
a                         #=> [1, 2, 3]


2542
2543
2544
2545
# File 'lib/source/ruby.rb', line 2542

def reject!
  `for(var i=0,l=this.length,temp=[];i<l;++i){try{if(!$T(#{yield `this[i]`})){temp.push(this[i]);};}catch(e){switch(e.__keyword__){case 'next':if(!$T(e.__return__)){temp.push(this[i]);};break;case 'break':return e.__return__;break;case 'redo':--i;break;default:throw(e);};};}`
  return `temp.length==l?nil:this._replace(temp)`
end

#replace(other) ⇒ Object

call-seq:

ary.replace(other) -> ary

Replaces the contents of ary with the contents of other, truncating or expanding if necessary.

a = %w(a b c)

a.replace(%w(w x y z))    #=> ["w", "x", "y", "z"]
a                         #=> ["w", "x", "y", "z"]


2558
2559
2560
# File 'lib/source/ruby.rb', line 2558

def replace(other)
  `this._replace(other)`
end

#reverseObject

call-seq:

ary.reverse -> array

Returns a new array containing ary’s elements in reverse order.

a = [1,2,3,4,5]

a.reverse   #=> [5, 4, 3, 2, 1]
a           #=> [1, 2, 3, 4, 5]


2572
2573
2574
2575
# File 'lib/source/ruby.rb', line 2572

def reverse
  `for(var i=this.length,result=[];i>0;){result.push(this[--i]);}`
  return `result`
end

#reverse!Object

call-seq:

ary.reverse! -> ary

Returns ary with its elements in reverse order.

a = [1,2,3,4,5]

a.reverse!    #=> [5, 4, 3, 2, 1]
a             #=> [5, 4, 3, 2, 1]


2587
2588
2589
# File 'lib/source/ruby.rb', line 2587

def reverse!
  `this.reverse()`
end

#reverse_eachObject

call-seq:

ary.reverse_each { |element| block } -> ary

Calls block once for each element in ary in reverse order, then returns ary.

%(a b c).reverse_each {|element| puts element.upcase }    #=> ["a", "b", "c"]

produces:

C
B
A


2605
2606
2607
2608
# File 'lib/source/ruby.rb', line 2605

def reverse_each
  `for(var i=this.length;i>0;){try{#{yield `this[--i]`};}catch(e){switch(e.__keyword__){case 'next':break;case 'break':return e.__return__;break;case 'redo':++i;break;default:throw(e);};};}`
  return self
end

#rindex(obj) ⇒ Object

call-seq:

ary.rindex(obj) -> integer or nil

Returns the highest index such that ary[index] == obj, or nil if obj is not found in ary.

a = %w(a b b b c)

a.rindex('b')   #=> 3
a.rindex('z')   #=> nil


2621
2622
2623
2624
# File 'lib/source/ruby.rb', line 2621

def rindex(obj)
  `for(var i=this.length;i>0;){if(this[--i].m$_eql2(obj)){return i;};}`
  return nil
end

#selectObject

call-seq:

ary.select { |element| block } -> array

Returns a new array containing only the items in ary for which block.call(element) evaluates to true.

[1,2,3,4,5].select {|x| x > 3 }   #=> [4, 5]


2634
2635
2636
2637
# File 'lib/source/ruby.rb', line 2634

def select
  `for(var i=0,l=this.length,result=[];i<l;++i){try{if($T(#{yield `this[i]`})){result.push(this[i]);};}catch(e){switch(e.__keyword__){case 'next':if($T(e.__return__)){result.push(this[i]);};break;case 'break':return e.__return__;break;case 'redo':--i;break;default:throw(e);};};}`
  return `result`
end

#shiftObject

call-seq:

ary.shift -> object or nil

Removes the first element of ary and returns it, shifting the indices of all other elements down by one. Returns nil if ary is empty.

a = %w(a b c)

a.shift   #=> "a"
a         #=> ["b", "c"]


2650
2651
2652
2653
# File 'lib/source/ruby.rb', line 2650

def shift
  `if(this.length==0){return nil;}`
  `this.shift()`
end

#sizeObject

call-seq:

ary.length -> integer
ary.size   -> integer

Returns the number of elements in ary. May be zero.

%w(a b c d e).size    #=> 5


2663
2664
2665
# File 'lib/source/ruby.rb', line 2663

def size
  `this.length`
end

#slice(index, length) ⇒ Object

call-seq:

ary[index]               -> object or nil
ary[start, length]       -> array or nil
ary[range]               -> array or nil
ary.slice(index)         -> object or nil
ary.slice(start, length) -> array or nil
ary.slice(range)         -> array or nil

Element Reference – returns the element at index, or an array of the elements in range or from start to start plus length. Returns nil if the index is out of range.

a = %w(a b c d e)

a[2] + a[0] + a[1]    #=> "cab"
a[6]                  #=> nil
a[1,2]                #=> ["b", "c"]
a[1..3]               #=> ["b", "c", "d"]
a[4..7]               #=> ["e"]
a[6..10]              #=> nil
a[-3,3]               #=> ["c", "d", "e"]
# special cases
a[5]                  #=> nil
a[5,1]                #=> []
a[5..10]              #=> []

FIX: Check so-called “special cases”



2694
2695
2696
# File 'lib/source/ruby.rb', line 2694

def slice(index, length)
  `c$Array.prototype.m$_brac.apply(this,arguments)`
end

#slice!(index, length) ⇒ Object

call-seq:

ary.slice!(index)         -> object or nil
ary.slice!(start, length) -> array or nil
ary.slice!(range)         -> array or nil

Deletes the element at index, or the series of elements in range or from start to start plus length. Returns the deleted object, subarray, or nil if the index is out of range.

a = %w(a b c d)

a.slice!(1)     #=> "b"
a               #=> ["a", "c", "d"]
a.slice!(-1)    #=> "d"
a               #=> ["a", "c"]
a.slice!(100)   #=> nil
a               #=> ["a", "c"]


2716
2717
2718
2719
2720
2721
2722
2723
# File 'lib/source/ruby.rb', line 2716

def slice!(index, length)
  `var l=this.length`
  `if(index.m$class()==c$Range){var start=index.__start__,end=index.__exclusive__?index.__end__-1:index.__end__;index=start<0?start+l:start;length=(end<0?end+l:end)-index+1;if(length<0){length=0};}else{if(index<0){index+=l;};if(length<0){throw('IndexError: negative length')};}`
  `if(index>=l){return nil;}`
  `if(index<0){throw('RangeError: out of range');}`
  `if($T(length)){if(length<=0){return [];};result=this.slice(index,index+length);this._replace(this.slice(0,index).concat(this.slice(index+length)));}else{result=this[index];this._replace(this.slice(0,index).concat(this.slice(index+1,l)));}`
  return `result`
end

#sort(block) ⇒ Object

call-seq:

ary.sort                 -> array
ary.sort { |a,b| block } -> array

Returns a new array containing the elements in ary sorted either by the <=> operator or by the optional block, which should compare a and b and return -1, 0, or 1. See also Enumerable#sort_by.

strings = %w(x z w y)                     #=> ["x", "z", "w", "y"]
symbols = strings.map {|x| x.to_sym }     #=> [:x, :z, :w, :y]

strings.sort                              #=> ["w", "x", "y", "z"]
symbols.sort {|a,b| b.to_s <=> a.to_s }   #=> [:z,:y,:x,:w]

FIX: Doesn’t handle loop control keywords



2740
2741
2742
# File 'lib/source/ruby.rb', line 2740

def sort(block)
  `c$Array.apply(null,this)._quick_sort(0,this.length,block)`
end

#sort!(block) ⇒ Object

call-seq:

ary.sort!                 -> ary
ary.sort! { |a,b| block } -> ary

Returns ary with its elements sorted either by the <=> operator or by the optional block, which should compare a and b and return -1, 0, or 1. See also Enumerable#sort_by.

a = [3, 2, 4, 1]

a.sort!                     #=> [1, 2, 3, 4]
a.sort! {|a,b| b <=> a }    #=> [4, 3, 2, 1]
a                           #=> [4, 3, 2, 1]

FIX: Doesn’t handle loop control keywords



2759
2760
2761
# File 'lib/source/ruby.rb', line 2759

def sort!(block)
  `this._quick_sort(0,this.length,block)`
end

#to_aObject

call-seq:

ary.to_a -> ary or array

Returns ary. If called on an instance of a subclass of Array, converts the receiver to an Array object.

[1,2,3].to_a    #=> [1, 2, 3]


2771
2772
2773
2774
# File 'lib/source/ruby.rb', line 2771

def to_a
  `if(this.m$class()==c$Array){return this;}`
  return `c$Array.apply(nil,this)`
end

#to_aryObject

call-seq:

ary.to_ary -> ary

Returns ary.

[1,2,3].to_ary    #=> [1, 2, 3]


2783
2784
2785
# File 'lib/source/ruby.rb', line 2783

def to_ary
  return self
end

#to_sObject

call-seq:

ary.to_s -> string

Returns ary.join.

%w(a b c).to_s    #=> "abc"


2794
2795
2796
# File 'lib/source/ruby.rb', line 2794

def to_s
  return self.join
end

#transposeObject

call-seq:

ary.transpose -> array

Assumes that ary contains only arrays of equal lengths and returns a new array with their rows and columns transposed.

[[1,2],[3,4],[5,6]].transpose   #=> [[1, 3, 5], [2, 4, 6]]


2806
2807
2808
2809
2810
2811
2812
# File 'lib/source/ruby.rb', line 2806

def transpose
  `if(this.length==0){return [];}`
  `var result=[],a=this[0].length,n=this.length`
  `while(result.length<a){result.push([])}`
  `for(var i=0;i<a;++i){for(var j=0;j<n;++j){if(this[j].length!=this[0].length){throw('IndexError: element size differs')};result[i][j]=this[j][i];};}`
  return `result`
end

#uniqObject

call-seq:

ary.uniq -> array

Returns a new array containing the elements of ary, with no duplicates.

%w(a b b c c c).uniq    #=> ["a", "b", "c"]


2821
2822
2823
2824
# File 'lib/source/ruby.rb', line 2821

def uniq
  `for(var i=0,l=this.length,result=[],seen={};i<l;++i){var a=this[i],k=a.m$hash();if(!seen[k]){seen[k]=true;result.push(a);};}`
  return `result`
end

#uniq!Object

call-seq:

ary.uniq! -> ary or nil

Returns ary with duplicate elements removed, or nil if no changes were made.

a = %w(a b b c c c)

a.uniq!   #=> ["a", "b", "c"]
a.uniq!   #=> nil
a         #=> ["a", "b", "c"]


2838
2839
2840
2841
# File 'lib/source/ruby.rb', line 2838

def uniq!
  `for(var i=0,l=this.length,result=[],seen={};i<l;++i){var a=this[i],k=a.m$hash();if(!seen[k]){seen[k]=true;result.push(a);};}`
  return `result.length==l?nil:this._replace(result)`
end

#unshift(*args) ⇒ Object

call-seq:

ary.unshift(obj, ...) -> ary

Prepends objects to the front of ary, shifting the indices of ary’s other elements up one, then returns ary.

a = %w(b c)

a.unshift('a')      #=> ["a", "b", "c"]
a.unshift(1,2,3)    #=> [1, 2, 3, "a", "b", "c"]


2854
2855
2856
2857
# File 'lib/source/ruby.rb', line 2854

def unshift(*args)
  `for(var i=args.length;i>0;){this.unshift(args[--i]);}`
  return self
end

#values_at(args) ⇒ Object

Returns an array containing the elements in self corresponding to the given selector(s). The selectors may be either integer indices or ranges. See also Array#select. FIX: Incomplete



2861
2862
# File 'lib/source/ruby.rb', line 2861

def values_at(args)
end

#zipObject

FIX: Incomplete



2865
2866
# File 'lib/source/ruby.rb', line 2865

def zip
end

#|(ary) ⇒ Object

call-seq:

ary | other -> array

Set Union – returns a new array by joining ary with other, removing duplicates.

[1,2,3] | [3,4,1]   #=> [1, 2, 3, 4]


1754
1755
1756
1757
1758
# File 'lib/source/ruby.rb', line 1754

def |(ary)
  `for(var i=0,l=this.length,result=[],seen={};i<l;++i){var a=this[i],k=a.m$hash();if(!seen[k]){seen[k]=true;result.push(a);};}`
  `for(var i=0,l=ary.length;i<l;++i){var a=ary[i],k=a.m$hash();if(!seen[k]){seen[k]=true;result.push(a);};}`
  return `result`
end