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]
1711 1712 1713 1714 |
# File 'lib/source/ruby.rb', line 1711 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]
1741 1742 1743 1744 1745 1746 |
# File 'lib/source/ruby.rb', line 1741 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]
1756 1757 1758 |
# File 'lib/source/ruby.rb', line 1756 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]
1768 1769 1770 1771 1772 |
# File 'lib/source/ruby.rb', line 1768 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]]
1782 1783 1784 1785 |
# File 'lib/source/ruby.rb', line 1782 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
1802 1803 1804 1805 |
# File 'lib/source/ruby.rb', line 1802 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
1818 1819 1820 1821 1822 |
# File 'lib/source/ruby.rb', line 1818 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] #=> []
1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 |
# File 'lib/source/ruby.rb', line 1850 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"]
1891 1892 1893 1894 1895 1896 1897 1898 1899 |
# File 'lib/source/ruby.rb', line 1891 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
1914 1915 1916 1917 |
# File 'lib/source/ruby.rb', line 1914 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"
1930 1931 1932 1933 1934 |
# File 'lib/source/ruby.rb', line 1930 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 #=> []
1946 1947 1948 1949 |
# File 'lib/source/ruby.rb', line 1946 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]
1964 1965 1966 1967 |
# File 'lib/source/ruby.rb', line 1964 def collect `for(var i=0,l=this.length,result=[];i<l;++i){try{result[i]=#{yield `this[i]`};}catch(e){switch(e._name){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]
1982 1983 1984 1985 |
# File 'lib/source/ruby.rb', line 1982 def collect! `for(var i=0,l=this.length;i<l;++i){try{this[i]=#{yield `this[i]`};}catch(e){switch(e._name){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"]
1994 1995 1996 1997 |
# File 'lib/source/ruby.rb', line 1994 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
2010 2011 2012 2013 2014 |
# File 'lib/source/ruby.rb', line 2010 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]
2023 2024 2025 2026 |
# File 'lib/source/ruby.rb', line 2023 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"
2043 2044 2045 2046 2047 |
# File 'lib/source/ruby.rb', line 2043 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
2061 2062 2063 2064 2065 2066 2067 2068 2069 |
# File 'lib/source/ruby.rb', line 2061 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"]
2081 2082 2083 2084 |
# File 'lib/source/ruby.rb', line 2081 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._name){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
2099 2100 2101 2102 |
# File 'lib/source/ruby.rb', line 2099 def each `for(var i=0,l=this.length;i<l;++i){try{#{yield `this[i]`};}catch(e){switch(e._name){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
2117 2118 2119 2120 |
# File 'lib/source/ruby.rb', line 2117 def each_index `for(var i=0,l=this.length;i<l;++i){try{#{yield `i`};}catch(e){switch(e._name){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
2130 2131 2132 |
# File 'lib/source/ruby.rb', line 2130 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
2145 2146 2147 2148 2149 |
# File 'lib/source/ruby.rb', line 2145 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"
2166 2167 2168 2169 2170 2171 |
# File 'lib/source/ruby.rb', line 2166 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
2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 |
# File 'lib/source/ruby.rb', line 2196 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"]
2221 2222 2223 2224 |
# File 'lib/source/ruby.rb', line 2221 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]
2239 2240 2241 2242 |
# File 'lib/source/ruby.rb', line 2239 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
2255 2256 2257 2258 |
# File 'lib/source/ruby.rb', line 2255 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
2274 2275 2276 2277 |
# File 'lib/source/ruby.rb', line 2274 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
2290 2291 2292 2293 |
# File 'lib/source/ruby.rb', line 2290 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]
2305 2306 2307 2308 2309 |
# File 'lib/source/ruby.rb', line 2305 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]"
2319 2320 2321 2322 |
# File 'lib/source/ruby.rb', line 2319 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"
2334 2335 2336 2337 2338 |
# File 'lib/source/ruby.rb', line 2334 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"]
2354 2355 2356 2357 2358 |
# File 'lib/source/ruby.rb', line 2354 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
2368 2369 2370 |
# File 'lib/source/ruby.rb', line 2368 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]
2385 2386 2387 2388 |
# File 'lib/source/ruby.rb', line 2385 def map `for(var i=0,l=this.length,result=[];i<l;++i){try{result[i]=#{yield `this[i]`};}catch(e){switch(e._name){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]
2403 2404 2405 2406 |
# File 'lib/source/ruby.rb', line 2403 def map! `for(var i=0,l=this.length;i<l;++i){try{this[i]=#{yield `this[i]`};}catch(e){switch(e._name){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
2415 2416 2417 2418 |
# File 'lib/source/ruby.rb', line 2415 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"]
2432 2433 2434 2435 |
# File 'lib/source/ruby.rb', line 2432 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]
2447 2448 2449 2450 |
# File 'lib/source/ruby.rb', line 2447 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
2465 2466 2467 2468 |
# File 'lib/source/ruby.rb', line 2465 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]
2481 2482 2483 2484 |
# File 'lib/source/ruby.rb', line 2481 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._name){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]
2498 2499 2500 2501 |
# File 'lib/source/ruby.rb', line 2498 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._name){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"]
2514 2515 2516 |
# File 'lib/source/ruby.rb', line 2514 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]
2528 2529 2530 |
# File 'lib/source/ruby.rb', line 2528 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]
2542 2543 2544 2545 |
# File 'lib/source/ruby.rb', line 2542 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
2561 2562 2563 2564 |
# File 'lib/source/ruby.rb', line 2561 def reverse_each `for(var i=this.length;i>0;){try{#{yield `this[--i]`};}catch(e){switch(e._name){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
2577 2578 2579 2580 |
# File 'lib/source/ruby.rb', line 2577 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]
2590 2591 2592 2593 |
# File 'lib/source/ruby.rb', line 2590 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._name){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"]
2606 2607 2608 2609 |
# File 'lib/source/ruby.rb', line 2606 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
2619 2620 2621 |
# File 'lib/source/ruby.rb', line 2619 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”
2650 2651 2652 |
# File 'lib/source/ruby.rb', line 2650 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"]
2672 2673 2674 2675 2676 2677 2678 2679 |
# File 'lib/source/ruby.rb', line 2672 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
2696 2697 2698 |
# File 'lib/source/ruby.rb', line 2696 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
2715 2716 2717 |
# File 'lib/source/ruby.rb', line 2715 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]
2727 2728 2729 2730 |
# File 'lib/source/ruby.rb', line 2727 def to_a `if(self.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]
2739 2740 2741 |
# File 'lib/source/ruby.rb', line 2739 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"
2750 2751 2752 |
# File 'lib/source/ruby.rb', line 2750 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]]
2762 2763 2764 2765 2766 2767 2768 |
# File 'lib/source/ruby.rb', line 2762 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"]
2777 2778 2779 2780 |
# File 'lib/source/ruby.rb', line 2777 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"]
2794 2795 2796 2797 |
# File 'lib/source/ruby.rb', line 2794 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"]
2810 2811 2812 2813 |
# File 'lib/source/ruby.rb', line 2810 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
2817 2818 |
# File 'lib/source/ruby.rb', line 2817 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]
1724 1725 1726 1727 1728 |
# File 'lib/source/ruby.rb', line 1724 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 |