Class: Array
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
-
#&(ary) ⇒ Object
call-seq: ary & other -> array.
-
#*(arg) ⇒ Object
call-seq: ary * num -> array ary * str -> string.
-
#+(ary) ⇒ Object
call-seq: ary + other -> array.
-
#-(ary) ⇒ Object
call-seq: ary - other -> array.
-
#<<(object) ⇒ Object
call-seq: ary << obj -> ary.
-
#<=>(ary) ⇒ Object
call-seq: ary <=> other -> -1, 0, 1.
-
#==(ary) ⇒ Object
call-seq: ary == other -> true or false.
- #[](index, length) ⇒ Object
- #[]=(index, length, object) ⇒ Object
-
#assoc(obj) ⇒ Object
call-seq: ary.assoc(obj) -> array or nil.
-
#at(index) ⇒ Object
call-seq: ary.at(index) -> object or nil.
-
#clear ⇒ Object
call-seq: ary.clear -> ary.
-
#collect ⇒ Object
call-seq: ary.collect { |element| block } -> array ary.map { |element| block } -> array.
-
#collect! ⇒ Object
call-seq: ary.collect! { |element| block } -> ary ary.map! { |element| block } -> ary.
-
#compact ⇒ Object
call-seq: ary.compact -> array.
-
#compact! ⇒ Object
call-seq: ary.compact! -> ary or nil.
-
#concat(ary) ⇒ Object
call-seq: ary.concat(other) -> ary.
-
#delete(obj) ⇒ Object
call-seq: ary.delete(obj) -> obj or nil ary.delete(obj) { block } -> obj or block.call.
-
#delete_at(index) ⇒ Object
call-seq: ary.delete_at(index) -> object or nil.
-
#delete_if ⇒ Object
call-seq: ary.delete_if { |element| block } -> ary.
-
#each ⇒ Object
call-seq: ary.each { |element| block } -> ary.
-
#each_index ⇒ Object
call-seq: ary.each_index { |index| block } -> ary.
-
#empty? ⇒ Boolean
call-seq: ary.empty? -> true or false.
-
#eql?(ary) ⇒ Boolean
call-seq: ary.eql?(other) -> true or false.
-
#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).
-
#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.
-
#first(n) ⇒ Object
call-seq: ary.first -> object or nil ary.first(n) -> array.
-
#flatten ⇒ Object
call-seq: ary.flatten -> array.
-
#flatten! ⇒ Object
call-seq: ary.flatten! -> ary or nil.
-
#hash ⇒ Object
:nodoc:.
-
#include?(obj) ⇒ Boolean
call-seq: ary.include?(obj) -> true or false.
-
#index(obj) ⇒ Object
call-seq: ary.index(obj) -> integer or nil.
-
#insert(index, *args) ⇒ Object
call-seq: ary.insert(index, obj…) -> ary.
-
#inspect ⇒ Object
call-seq: ary.inspect -> string.
-
#join(str = '') ⇒ Object
call-seq: ary.join(str = ”) -> string.
-
#last(n) ⇒ Object
call-seq: ary.last -> object or nil ary.last(n) -> array.
-
#length ⇒ Object
call-seq: ary.length -> integer ary.size -> integer.
-
#map ⇒ Object
call-seq: ary.collect { |element| block } -> array ary.map { |element| block } -> array.
-
#map! ⇒ Object
call-seq: ary.collect! { |element| block } -> ary ary.map! { |element| block } -> ary.
-
#nitems ⇒ Object
call-seq: ary.nitems -> integer.
-
#pop ⇒ Object
call-seq: ary.pop -> object or nil.
-
#push(*args) ⇒ Object
call-seq: ary.push(obj, …) -> ary.
-
#rassoc(obj) ⇒ Object
call-seq: ary.rassoc(obj) -> array or nil.
-
#reject ⇒ Object
call-seq: ary.reject { |element| block } -> array.
-
#reject! ⇒ Object
call-seq: ary.reject! { |element| block } -> ary or nil.
-
#replace(other) ⇒ Object
call-seq: ary.replace(other) -> ary.
-
#reverse ⇒ Object
call-seq: ary.reverse -> array.
-
#reverse! ⇒ Object
call-seq: ary.reverse! -> ary.
-
#reverse_each ⇒ Object
call-seq: ary.reverse_each { |element| block } -> ary.
-
#rindex(obj) ⇒ Object
call-seq: ary.rindex(obj) -> integer or nil.
-
#select ⇒ Object
call-seq: ary.select { |element| block } -> array.
-
#shift ⇒ Object
call-seq: ary.shift -> object or nil.
-
#size ⇒ Object
call-seq: ary.length -> integer ary.size -> integer.
- #slice(index, length) ⇒ Object
-
#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.
-
#sort(block) ⇒ Object
call-seq: ary.sort -> array ary.sort { |a,b| block } -> array.
-
#sort!(block) ⇒ Object
call-seq: ary.sort! -> ary ary.sort! { |a,b| block } -> ary.
-
#to_a ⇒ Object
call-seq: ary.to_a -> ary or array.
-
#to_ary ⇒ Object
call-seq: ary.to_ary -> ary.
-
#to_s ⇒ Object
call-seq: ary.to_s -> string.
-
#transpose ⇒ Object
call-seq: ary.transpose -> array.
-
#uniq ⇒ Object
call-seq: ary.uniq -> array.
-
#uniq! ⇒ Object
call-seq: ary.uniq! -> ary or nil.
-
#unshift(*args) ⇒ Object
call-seq: ary.unshift(obj, …) -> ary.
-
#values_at(args) ⇒ Object
Returns an array containing the elements in self corresponding to the given selector(s).
-
#zip ⇒ Object
FIX: Incomplete.
-
#|(ary) ⇒ Object
call-seq: ary | other -> array.
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]
1724 1725 1726 1727 |
# File 'lib/source/ruby.rb', line 1724 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]
1754 1755 1756 1757 1758 1759 |
# File 'lib/source/ruby.rb', line 1754 def *(arg) `if(arg.m$class()==c$String){return 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]
1769 1770 1771 |
# File 'lib/source/ruby.rb', line 1769 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]
1781 1782 1783 1784 1785 |
# File 'lib/source/ruby.rb', line 1781 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]]
1795 1796 1797 1798 |
# File 'lib/source/ruby.rb', line 1795 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
1815 1816 1817 1818 |
# File 'lib/source/ruby.rb', line 1815 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
1831 1832 1833 1834 1835 |
# File 'lib/source/ruby.rb', line 1831 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) ⇒ 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] #=> []
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 |
# File 'lib/source/ruby.rb', line 1863 def [](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(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"]
1904 1905 1906 1907 1908 1909 1910 1911 1912 |
# File 'lib/source/ruby.rb', line 1904 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 |
#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
1927 1928 1929 1930 |
# File 'lib/source/ruby.rb', line 1927 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"
1943 1944 1945 1946 1947 |
# File 'lib/source/ruby.rb', line 1943 def at(index) `if(index<0){index+=this.length;}` `if(index<0||index>=this.length){return nil;}` return `this[index]` end |
#clear ⇒ Object
call-seq:
ary.clear -> ary
Removes all elements from ary.
a = %w(a b c d e)
a.clear #=> []
a #=> []
1959 1960 1961 1962 |
# File 'lib/source/ruby.rb', line 1959 def clear `this.length=0` return self end |
#collect ⇒ Object
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]
1977 1978 1979 1980 |
# File 'lib/source/ruby.rb', line 1977 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._value;break;case 'break':return e._value;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]
1995 1996 1997 1998 |
# File 'lib/source/ruby.rb', line 1995 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._value;break;case 'break':return e._value;break;case 'redo':--i;break;default:throw(e);};};}` return self end |
#compact ⇒ Object
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"]
2007 2008 2009 2010 |
# File 'lib/source/ruby.rb', line 2007 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
2023 2024 2025 2026 2027 |
# File 'lib/source/ruby.rb', line 2023 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]
2036 2037 2038 2039 |
# File 'lib/source/ruby.rb', line 2036 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"
2056 2057 2058 2059 2060 |
# File 'lib/source/ruby.rb', line 2056 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?(blockGivenBool?#{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
2074 2075 2076 2077 2078 2079 2080 2081 2082 |
# File 'lib/source/ruby.rb', line 2074 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_if ⇒ Object
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"]
2094 2095 2096 2097 |
# File 'lib/source/ruby.rb', line 2094 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._value)){temp.push(this[i]);};break;case 'break':return e._value;break;case 'redo':--i;break;default:throw(e);};};}` `this._replace(temp)` end |
#each ⇒ Object
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
2112 2113 2114 2115 |
# File 'lib/source/ruby.rb', line 2112 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._value;break;case 'redo':--i;break;default:throw(e);};};}` return self end |
#each_index ⇒ Object
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
2130 2131 2132 2133 |
# File 'lib/source/ruby.rb', line 2130 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._value;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
2143 2144 2145 |
# File 'lib/source/ruby.rb', line 2143 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
2158 2159 2160 2161 2162 |
# File 'lib/source/ruby.rb', line 2158 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$eqlBool(ary[i]))){return false;};}` return true 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"
2179 2180 2181 2182 2183 2184 |
# File 'lib/source/ruby.rb', line 2179 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
2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 |
# File 'lib/source/ruby.rb', line 2209 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"]
2234 2235 2236 2237 |
# File 'lib/source/ruby.rb', line 2234 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 |
#flatten ⇒ Object
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]
2252 2253 2254 2255 |
# File 'lib/source/ruby.rb', line 2252 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
2268 2269 2270 2271 |
# File 'lib/source/ruby.rb', line 2268 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$flattenBang());}else{result.push(this[i]);};}` return `this.length==result.length?nil:this._replace(result)` 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
2287 2288 2289 2290 |
# File 'lib/source/ruby.rb', line 2287 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
2303 2304 2305 2306 |
# File 'lib/source/ruby.rb', line 2303 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]
2318 2319 2320 2321 2322 |
# File 'lib/source/ruby.rb', line 2318 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 |
#inspect ⇒ Object
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]"
2332 2333 2334 2335 |
# File 'lib/source/ruby.rb', line 2332 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"
2347 2348 2349 2350 2351 |
# File 'lib/source/ruby.rb', line 2347 def join(str = '') `var result=this[0]!=null?this[0].m$join?this[0].m$join(str._value)._value:this[0].m$toS()._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$toS()._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"]
2367 2368 2369 2370 2371 |
# File 'lib/source/ruby.rb', line 2367 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 |
#length ⇒ Object
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
2381 2382 2383 |
# File 'lib/source/ruby.rb', line 2381 def length `this.length` end |
#map ⇒ Object
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]
2398 2399 2400 2401 |
# File 'lib/source/ruby.rb', line 2398 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._value;break;case 'break':return e._value;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]
2416 2417 2418 2419 |
# File 'lib/source/ruby.rb', line 2416 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._value;break;case 'break':return e._value;break;case 'redo':--i;break;default:throw(e);};};}` return self end |
#nitems ⇒ Object
call-seq:
ary.nitems -> integer
Returns the number of non-nil elements in ary. May be zero.
[1, nil, 3, nil, 5].nitems #=> 3
2428 2429 2430 2431 |
# File 'lib/source/ruby.rb', line 2428 def nitems `for(var i=0,l=this.length,result=0;i<l;++i){if(this[i]!==nil){result++;};}` return `result` end |
#pop ⇒ Object
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"]
2445 2446 2447 2448 |
# File 'lib/source/ruby.rb', line 2445 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]
2460 2461 2462 2463 |
# File 'lib/source/ruby.rb', line 2460 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
2478 2479 2480 2481 |
# File 'lib/source/ruby.rb', line 2478 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 |
#reject ⇒ Object
2494 2495 2496 2497 |
# File 'lib/source/ruby.rb', line 2494 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._value)){result.push(this[i]);};break;case 'break':return e._value;break;case 'redo':--i;break;default:throw(e);};};}` return `result` end |
#reject! ⇒ Object
2511 2512 2513 2514 |
# File 'lib/source/ruby.rb', line 2511 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._value)){temp.push(this[i]);};break;case 'break':return e._value;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"]
2527 2528 2529 |
# File 'lib/source/ruby.rb', line 2527 def replace(other) `this._replace(other)` end |
#reverse ⇒ Object
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]
2541 2542 2543 |
# File 'lib/source/ruby.rb', line 2541 def reverse `this.reverse()` 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]
2555 2556 2557 2558 |
# File 'lib/source/ruby.rb', line 2555 def reverse! `for(var i=0,l=this.length,last=l-1;i<l;++i){j=last-i;if(i>=j){break;};this._swap(i,j);}` return self end |
#reverse_each ⇒ Object
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
2574 2575 2576 2577 |
# File 'lib/source/ruby.rb', line 2574 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._value;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
2590 2591 2592 2593 |
# File 'lib/source/ruby.rb', line 2590 def rindex(obj) `for(var i=this.length;i>0;){if(this[--i].m$_eql2(obj)){return i;};}` return nil end |
#select ⇒ Object
2603 2604 2605 2606 |
# File 'lib/source/ruby.rb', line 2603 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._value)){result.push(this[i]);};break;case 'break':return e._value;break;case 'redo':--i;break;default:throw(e);};};}` return `result` end |
#shift ⇒ Object
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"]
2619 2620 2621 2622 |
# File 'lib/source/ruby.rb', line 2619 def shift `if(this.length==0){return nil;}` `this.shift()` end |
#size ⇒ Object
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
2632 2633 2634 |
# File 'lib/source/ruby.rb', line 2632 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”
2663 2664 2665 |
# File 'lib/source/ruby.rb', line 2663 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"]
2685 2686 2687 2688 2689 2690 2691 2692 |
# File 'lib/source/ruby.rb', line 2685 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
2709 2710 2711 |
# File 'lib/source/ruby.rb', line 2709 def sort(block) `c$Array.apply(null,this)._quickSort(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
2728 2729 2730 |
# File 'lib/source/ruby.rb', line 2728 def sort!(block) `this._quickSort(0,this.length,block)` end |
#to_a ⇒ Object
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]
2740 2741 2742 2743 |
# File 'lib/source/ruby.rb', line 2740 def to_a `if(this.m$class()==c$Array){return this;}` return `c$Array.apply(nil,this)` end |
#to_ary ⇒ Object
call-seq:
ary.to_ary -> ary
Returns ary.
[1,2,3].to_ary #=> [1, 2, 3]
2752 2753 2754 |
# File 'lib/source/ruby.rb', line 2752 def to_ary return self end |
#to_s ⇒ Object
call-seq:
ary.to_s -> string
Returns ary.join.
%w(a b c).to_s #=> "abc"
2763 2764 2765 |
# File 'lib/source/ruby.rb', line 2763 def to_s return self.join end |
#transpose ⇒ Object
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]]
2775 2776 2777 2778 2779 2780 2781 |
# File 'lib/source/ruby.rb', line 2775 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 |
#uniq ⇒ Object
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"]
2790 2791 2792 2793 |
# File 'lib/source/ruby.rb', line 2790 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"]
2807 2808 2809 2810 |
# File 'lib/source/ruby.rb', line 2807 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"]
2823 2824 2825 2826 |
# File 'lib/source/ruby.rb', line 2823 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
2830 2831 |
# File 'lib/source/ruby.rb', line 2830 def values_at(args) 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]
1737 1738 1739 1740 1741 |
# File 'lib/source/ruby.rb', line 1737 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 |