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]
1730 1731 1732 1733 |
# File 'lib/source/ruby.rb', line 1730 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]
1760 1761 1762 1763 1764 1765 |
# File 'lib/source/ruby.rb', line 1760 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]
1775 1776 1777 |
# File 'lib/source/ruby.rb', line 1775 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]
1787 1788 1789 1790 1791 |
# File 'lib/source/ruby.rb', line 1787 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]]
1801 1802 1803 1804 |
# File 'lib/source/ruby.rb', line 1801 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
1821 1822 1823 1824 |
# File 'lib/source/ruby.rb', line 1821 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
1837 1838 1839 1840 1841 |
# File 'lib/source/ruby.rb', line 1837 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] #=> []
1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 |
# File 'lib/source/ruby.rb', line 1869 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"]
1910 1911 1912 1913 1914 1915 1916 1917 1918 |
# File 'lib/source/ruby.rb', line 1910 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
1933 1934 1935 1936 |
# File 'lib/source/ruby.rb', line 1933 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"
1949 1950 1951 1952 1953 |
# File 'lib/source/ruby.rb', line 1949 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 #=> []
1965 1966 1967 1968 |
# File 'lib/source/ruby.rb', line 1965 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]
1983 1984 1985 1986 |
# File 'lib/source/ruby.rb', line 1983 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]
2001 2002 2003 2004 |
# File 'lib/source/ruby.rb', line 2001 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"]
2013 2014 2015 2016 |
# File 'lib/source/ruby.rb', line 2013 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
2029 2030 2031 2032 2033 |
# File 'lib/source/ruby.rb', line 2029 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]
2042 2043 2044 2045 |
# File 'lib/source/ruby.rb', line 2042 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"
2062 2063 2064 2065 2066 |
# File 'lib/source/ruby.rb', line 2062 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
2080 2081 2082 2083 2084 2085 2086 2087 2088 |
# File 'lib/source/ruby.rb', line 2080 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"]
2100 2101 2102 2103 |
# File 'lib/source/ruby.rb', line 2100 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
2118 2119 2120 2121 |
# File 'lib/source/ruby.rb', line 2118 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
2136 2137 2138 2139 |
# File 'lib/source/ruby.rb', line 2136 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
2149 2150 2151 |
# File 'lib/source/ruby.rb', line 2149 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
2164 2165 2166 2167 2168 |
# File 'lib/source/ruby.rb', line 2164 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"
2185 2186 2187 2188 2189 2190 |
# File 'lib/source/ruby.rb', line 2185 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
2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 |
# File 'lib/source/ruby.rb', line 2215 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"]
2240 2241 2242 2243 |
# File 'lib/source/ruby.rb', line 2240 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]
2258 2259 2260 2261 |
# File 'lib/source/ruby.rb', line 2258 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
2274 2275 2276 2277 |
# File 'lib/source/ruby.rb', line 2274 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
2293 2294 2295 2296 |
# File 'lib/source/ruby.rb', line 2293 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
2309 2310 2311 2312 |
# File 'lib/source/ruby.rb', line 2309 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]
2324 2325 2326 2327 2328 |
# File 'lib/source/ruby.rb', line 2324 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]"
2338 2339 2340 2341 |
# File 'lib/source/ruby.rb', line 2338 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"
2353 2354 2355 2356 2357 |
# File 'lib/source/ruby.rb', line 2353 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"]
2373 2374 2375 2376 2377 |
# File 'lib/source/ruby.rb', line 2373 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
2387 2388 2389 |
# File 'lib/source/ruby.rb', line 2387 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]
2404 2405 2406 2407 |
# File 'lib/source/ruby.rb', line 2404 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]
2422 2423 2424 2425 |
# File 'lib/source/ruby.rb', line 2422 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
2434 2435 2436 2437 |
# File 'lib/source/ruby.rb', line 2434 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"]
2451 2452 2453 2454 |
# File 'lib/source/ruby.rb', line 2451 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]
2466 2467 2468 2469 |
# File 'lib/source/ruby.rb', line 2466 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
2484 2485 2486 2487 |
# File 'lib/source/ruby.rb', line 2484 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
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]
2500 2501 2502 2503 |
# File 'lib/source/ruby.rb', line 2500 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
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]
2517 2518 2519 2520 |
# File 'lib/source/ruby.rb', line 2517 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"]
2533 2534 2535 |
# File 'lib/source/ruby.rb', line 2533 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]
2547 2548 2549 |
# File 'lib/source/ruby.rb', line 2547 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]
2561 2562 2563 2564 |
# File 'lib/source/ruby.rb', line 2561 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
2580 2581 2582 2583 |
# File 'lib/source/ruby.rb', line 2580 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
2596 2597 2598 2599 |
# File 'lib/source/ruby.rb', line 2596 def rindex(obj) `for(var i=this.length;i>0;){if(this[--i].m$_eql2(obj)){return i;};}` return nil end |
#select ⇒ Object
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]
2609 2610 2611 2612 |
# File 'lib/source/ruby.rb', line 2609 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"]
2625 2626 2627 2628 |
# File 'lib/source/ruby.rb', line 2625 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
2638 2639 2640 |
# File 'lib/source/ruby.rb', line 2638 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”
2669 2670 2671 |
# File 'lib/source/ruby.rb', line 2669 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"]
2691 2692 2693 2694 2695 2696 2697 2698 |
# File 'lib/source/ruby.rb', line 2691 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
2715 2716 2717 |
# File 'lib/source/ruby.rb', line 2715 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
2734 2735 2736 |
# File 'lib/source/ruby.rb', line 2734 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]
2746 2747 2748 2749 |
# File 'lib/source/ruby.rb', line 2746 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]
2758 2759 2760 |
# File 'lib/source/ruby.rb', line 2758 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"
2769 2770 2771 |
# File 'lib/source/ruby.rb', line 2769 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]]
2781 2782 2783 2784 2785 2786 2787 |
# File 'lib/source/ruby.rb', line 2781 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"]
2796 2797 2798 2799 |
# File 'lib/source/ruby.rb', line 2796 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"]
2813 2814 2815 2816 |
# File 'lib/source/ruby.rb', line 2813 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"]
2829 2830 2831 2832 |
# File 'lib/source/ruby.rb', line 2829 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
2836 2837 |
# File 'lib/source/ruby.rb', line 2836 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]
1743 1744 1745 1746 1747 |
# File 'lib/source/ruby.rb', line 1743 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 |