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 = nil) ⇒ Object
- #[]=(index, length, object) ⇒ Object
-
#__id__ ⇒ Object
:nodoc:.
-
#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.
-
#equal?(ary) ⇒ Boolean
:nodoc:.
-
#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.
-
#object_id ⇒ Object
:nodoc:.
-
#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]
1742 1743 1744 1745 |
# File 'lib/source/ruby.rb', line 1742 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]
1772 1773 1774 1775 1776 1777 |
# File 'lib/source/ruby.rb', line 1772 def *(arg) `if(arg.m$class()==c$String){return $q(this.join(arg));}` `var result=[],i=0,l=parseInt(arg)` `while(i<l){result=result.concat(this);i++;}` return `result` end |
#+(ary) ⇒ Object
call-seq:
ary + other -> array
Concatenation – returns a new array built by concatenating ary and other.
[1,2,3] + [4,5] #=> [1, 2, 3, 4, 5]
1787 1788 1789 |
# File 'lib/source/ruby.rb', line 1787 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]
1799 1800 1801 1802 1803 |
# File 'lib/source/ruby.rb', line 1799 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]]
1813 1814 1815 1816 |
# File 'lib/source/ruby.rb', line 1813 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
1833 1834 1835 1836 |
# File 'lib/source/ruby.rb', line 1833 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
1849 1850 1851 1852 1853 |
# File 'lib/source/ruby.rb', line 1849 def ==(ary) `if(ary.m$class()!==c$Array||ary.length!==this.length){return false;}` `for(var i=0,l=this.length;i<l;++i){if(!(this[i].m$_eql2(ary[i]))){return false;};}` return true end |
#[](index, length = nil) ⇒ Object
call-seq:
ary[index] -> object or nil
ary[start, length] -> array or nil
ary[range] -> array or nil
ary.slice(index) -> object or nil
ary.slice(start, length) -> array or nil
ary.slice(range) -> array or nil
Element Reference – returns the element at index, or an array of the elements in range or from start to start plus length. Returns nil
if the index is out of range.
a = %w(a b c d e)
a[2] + a[0] + a[1] #=> "cab"
a[6] #=> nil
a[1,2] #=> ["b", "c"]
a[1..3] #=> ["b", "c", "d"]
a[4..7] #=> ["e"]
a[6..10] #=> nil
a[-3,3] #=> ["c", "d", "e"]
# special cases
a[5] #=> nil
a[5,1] #=> []
a[5..10] #=> []
1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 |
# File 'lib/source/ruby.rb', line 1881 def [](index, length = nil) `var l=this.length` `if(index.m$class()==c$Range){ var start=index.__start__,end=index.__exclusive__?index.__end__-1:index.__end__; index=start<0?start+l:start; length=(end<0?end+l:end)-index+1; if(length<0){length=0}; }else{ if(index<0){index+=l;}; }` `if(index>=l||index<0){return nil;}` `if($T(length)){ if(length<=0){return [];}; result=this.slice(index,index+length); }else{ result=this[index]; }` return `result` end |
#[]=(index, length, object) ⇒ Object
call-seq:
ary[index] = obj -> obj
ary[start, length] = obj or array or nil -> obj or array or nil
ary[range] = obj or array or nil -> obj or array or nil
Element Assignment – sets the element at _index, or replaces the elements in range or from start to start plus length, truncating or expanding ary as necessary. If nil
is used in the second and third form, deletes elements. See also Array#push
, and Array#unshift
.
a = []
a[4] = '4' #=> [nil, nil, nil, nil, "4"]
a[0,3] = %w(a b c) #=> ["a", "b", "c", nil, "4"]
a[1..2] = [1,2] #=> ["a", 1, 2, nil, "4"]
a[0,2] = '?' #=> ["?", 2, nil, "4"]
a[0..2] = 'A' #=> ["A", "4"]
a[-1] = 'Z' #=> ["A", "Z"]
a[1..-1] = nil #=> ["A"]
1922 1923 1924 1925 1926 1927 1928 1929 1930 |
# File 'lib/source/ruby.rb', line 1922 def []=(index, length, object) `var l=this.length` `if(object==null){object=length;length=$u;}` `if(index.m$class()==c$Range){var start=index.__start__,end=index.__exclusive__?index.__end__-1:index.__end__;index=start<0?start+l:start;length=(end<0?end+l:end)-index+1;if(length<0){length=0};}else{if(index<0){index+=l;};if(length<0){throw('IndexError: negative length')}}` `if(index<0){throw('RangeError: out of range');}` `while(this.length<index){this.push(nil);}` `if($T(length)){var l=this.length,final=(index+length>l)?l:index+length;this._replace(this.slice(0,index).concat(object===nil?[]:(object.m$class()==c$Array?object:[object])).concat(this.slice(final,l)))}else{this[index]=object}` return `object` end |
#__id__ ⇒ Object
:nodoc:
1932 1933 1934 |
# File 'lib/source/ruby.rb', line 1932 def __id__ # :nodoc: `this.__id__||(this.__id__=Red.id++)` end |
#assoc(obj) ⇒ Object
call-seq:
ary.assoc(obj) -> array or nil
Searches through ary, comparing obj with the first element of each ary element that is also an array. Returns the first array such that array[0] == obj
, or nil
if no match is found. See also Array#rassoc
.
a = [%w(colors red blue green), %w(letters a b c), 'foo']
a.assoc('letters') #=> ["letters", "a", "b", "c"]
a.assoc('foo') #=> nil
1949 1950 1951 1952 |
# File 'lib/source/ruby.rb', line 1949 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"
1965 1966 1967 1968 1969 |
# File 'lib/source/ruby.rb', line 1965 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 #=> []
1981 1982 1983 1984 |
# File 'lib/source/ruby.rb', line 1981 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]
1999 2000 2001 2002 |
# File 'lib/source/ruby.rb', line 1999 def collect `for(var i=0,l=this.length,result=[];i<l;++i){try{result[i]=#{yield `this[i]`};}catch(e){switch(e.__keyword__){case 'next':result[i]=e.__return__;break;case 'break':return e.__return__;break;case 'redo':--i;break;default:throw(e);};};}` return `result` end |
#collect! ⇒ Object
call-seq:
ary.collect! { |element| block } -> ary
ary.map! { |element| block } -> ary
Calls block once for each element in ary, replacing the element with the value returned by the block, then returns ary. See also Enumerable#collect
.
a = [1,2,3,4]
a.collect! {|x| x + 100 } #=> [101, 102, 103, 104]
a.collect! {|x| x + 100 } #=> [201, 202, 203, 204]
2017 2018 2019 2020 |
# File 'lib/source/ruby.rb', line 2017 def collect! `for(var i=0,l=this.length;i<l;++i){try{this[i]=#{yield `this[i]`};}catch(e){switch(e.__keyword__){case 'next':this[i]=e.__return__;break;case 'break':return e.__return__;break;case 'redo':--i;break;default:throw(e);};};}` return self end |
#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"]
2029 2030 2031 2032 |
# File 'lib/source/ruby.rb', line 2029 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
2045 2046 2047 2048 2049 |
# File 'lib/source/ruby.rb', line 2045 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]
2058 2059 2060 2061 |
# File 'lib/source/ruby.rb', line 2058 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"
2078 2079 2080 2081 2082 |
# File 'lib/source/ruby.rb', line 2078 def delete(obj) `for(var i=0,l=this.length,temp=[];i<l;++i){if(!(this[i].m$_eql2(obj))){temp.push(this[i]);};}` `this._replace(temp)` return `l===this.length?(#{block_given?}?#{yield}:nil):obj` end |
#delete_at(index) ⇒ Object
call-seq:
ary.delete_at(index) -> object or nil
Deletes the element at the specified index, returning that element or nil
if the index is out of range. See also Array#slice!
.
a = %w(a b c d)
a.delete_at(2) #=> "c"
a #=> ["a", "b", "d"]
a.delete_at(99) #=> nil
2096 2097 2098 2099 2100 2101 2102 2103 2104 |
# File 'lib/source/ruby.rb', line 2096 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"]
2116 2117 2118 2119 |
# File 'lib/source/ruby.rb', line 2116 def delete_if `for(var temp=[],i=0,l=this.length;i<l;++i){try{if(!$T(#{yield `this[i]`})){temp.push(this[i]);};}catch(e){switch(e.__keyword__){case 'next':if(!$T(e.__return__)){temp.push(this[i]);};break;case 'break':return e.__return__;break;case 'redo':--i;break;default:throw(e);};};}` `this._replace(temp)` end |
#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
2134 2135 2136 2137 |
# File 'lib/source/ruby.rb', line 2134 def each `for(var i=0,l=this.length;i<l;++i){try{#{yield `this[i]`};}catch(e){switch(e.__keyword__){case 'next':break;case 'break':return e.__return__;break;case 'redo':--i;break;default:throw(e);};};}` return self end |
#each_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
2152 2153 2154 2155 |
# File 'lib/source/ruby.rb', line 2152 def each_index `for(var i=0,l=this.length;i<l;++i){try{#{yield `i`};}catch(e){switch(e.__keyword__){case 'next':break;case 'break':return e.__return__;break;case 'redo':--i;break;default:throw(e);};};}` return self end |
#empty? ⇒ Boolean
call-seq:
ary.empty? -> true or false
Returns true
if ary contains no elements.
[''].empty? #=> false
[].empty? #=> true
2165 2166 2167 |
# File 'lib/source/ruby.rb', line 2165 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
2180 2181 2182 2183 2184 |
# File 'lib/source/ruby.rb', line 2180 def eql?(ary) `if(ary.m$class()!==c$Array||ary.length!==this.length){return false;}` `for(var i=0,l=this.length;i<l;++i){if(!(this[i].m$eql_bool(ary[i]))){return false;};}` return true end |
#equal?(ary) ⇒ Boolean
:nodoc:
2186 2187 2188 2189 |
# File 'lib/source/ruby.rb', line 2186 def equal?(ary) # :nodoc: `var thisId=this.__id__||(this.__id__=Red.id++),aryId=ary.__id__||(ary.__id__=Red.id++)` `thisId==aryId` end |
#fetch(index, &block) ⇒ Object
call-seq:
ary.fetch(index) -> object or nil
ary.fetch(index, default) -> object or default
ary.fetch(index) { |index| block } -> object or block.call(index)
Tries to return the element at position index. Negative values of index count from the end of the array.
a = [100,101,102,103]
a.fetch(1) #=> 101
a.fetch(-1) #=> 103
a.fetch(5, 'FAIL') #=> "FAIL"
a.fetch(5) {|i| "FAIL: #{i}" } #=> "FAIL: 5"
2206 2207 2208 2209 2210 2211 |
# File 'lib/source/ruby.rb', line 2206 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
2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 |
# File 'lib/source/ruby.rb', line 2236 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"]
2261 2262 2263 2264 |
# File 'lib/source/ruby.rb', line 2261 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]
2279 2280 2281 2282 |
# File 'lib/source/ruby.rb', line 2279 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
2295 2296 2297 2298 2299 |
# File 'lib/source/ruby.rb', line 2295 def flatten! `var flatten=function(ary){for(var i=0,l=ary.length,result=[];i<l;++i){if(ary[i].m$class()==c$Array){result=result.concat(ary[i].m$flatten());}else{result.push(ary[i]);};};return result;}` `for(var i=0,l=this.length,result=[];i<l;++i){if(this[i].m$class()==c$Array){result=result.concat(flatten(this[i]));}else{result.push(this[i]);};}` return `l==result.length?nil:this._replace(result)` end |
#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
2315 2316 2317 2318 |
# File 'lib/source/ruby.rb', line 2315 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
2331 2332 2333 2334 |
# File 'lib/source/ruby.rb', line 2331 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]
2346 2347 2348 2349 2350 |
# File 'lib/source/ruby.rb', line 2346 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]"
2360 2361 2362 2363 |
# File 'lib/source/ruby.rb', line 2360 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"
2375 2376 2377 2378 2379 |
# File 'lib/source/ruby.rb', line 2375 def join(str = '') `var result=this[0]!=null?this[0].m$join?this[0].m$join(str.__value__).__value__:this[0].m$to_s().__value__:''` `for(var i=1,l=this.length;i<l;++i){result+=(str.__value__||'')+(this[i].m$join?this[i].m$join(str).__value__:this[i].m$to_s().__value__);}` return `$q(result)` end |
#last(n) ⇒ Object
call-seq:
ary.last -> object or nil
ary.last(n) -> array
Returns the last element of ary, or an array of the last n elements of ary. If ary is empty, the first form returns nil
and the second form returns an empty array.
a = %w(a b c d)
a.last #=> "d"
a.last(1) #=> ["d"]
a.last(3) #=> ["b", "c", "d"]
2395 2396 2397 2398 2399 |
# File 'lib/source/ruby.rb', line 2395 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
2409 2410 2411 |
# File 'lib/source/ruby.rb', line 2409 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]
2426 2427 2428 2429 |
# File 'lib/source/ruby.rb', line 2426 def map `for(var i=0,l=this.length,result=[];i<l;++i){try{result[i]=#{yield `this[i]`};}catch(e){switch(e.__keyword__){case 'next':result[i]=e.__return__;break;case 'break':return e.__return__;break;case 'redo':--i;break;default:throw(e);};};}` return `result` end |
#map! ⇒ Object
call-seq:
ary.collect! { |element| block } -> ary
ary.map! { |element| block } -> ary
Calls block once for each element in ary, replacing the element with the value returned by the block, then returns ary. See also Enumerable#collect
.
a = [1,2,3,4]
a.map! {|x| x + 100 } #=> [101, 102, 103, 104]
a.map! {|x| x + 100 } #=> [201, 202, 203, 204]
2444 2445 2446 2447 |
# File 'lib/source/ruby.rb', line 2444 def map! `for(var i=0,l=this.length;i<l;++i){try{this[i]=#{yield `this[i]`};}catch(e){switch(e.__keyword__){case 'next':this[i]=e.__return__;break;case 'break':return e.__return__;break;case 'redo':--i;break;default:throw(e);};};}` return self end |
#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
2456 2457 2458 2459 |
# File 'lib/source/ruby.rb', line 2456 def nitems `for(var i=0,l=this.length,result=0;i<l;++i){if(this[i]!==nil){result++;};}` return `result` end |
#object_id ⇒ Object
:nodoc:
2461 2462 2463 |
# File 'lib/source/ruby.rb', line 2461 def object_id # :nodoc: `this.__id__||(this.__id__=Red.id++)` 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"]
2477 2478 2479 2480 |
# File 'lib/source/ruby.rb', line 2477 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]
2492 2493 2494 2495 |
# File 'lib/source/ruby.rb', line 2492 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
2510 2511 2512 2513 |
# File 'lib/source/ruby.rb', line 2510 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
2526 2527 2528 2529 |
# File 'lib/source/ruby.rb', line 2526 def reject `for(var i=0,l=this.length,result=[];i<l;++i){try{if(!$T(#{yield `this[i]`})){result.push(this[i]);};}catch(e){switch(e.__keyword__){case 'next':if(!$T(e.__return__)){result.push(this[i]);};break;case 'break':return e.__return__;break;case 'redo':--i;break;default:throw(e);};};}` return `result` end |
#reject! ⇒ Object
2543 2544 2545 2546 |
# File 'lib/source/ruby.rb', line 2543 def reject! `for(var i=0,l=this.length,temp=[];i<l;++i){try{if(!$T(#{yield `this[i]`})){temp.push(this[i]);};}catch(e){switch(e.__keyword__){case 'next':if(!$T(e.__return__)){temp.push(this[i]);};break;case 'break':return e.__return__;break;case 'redo':--i;break;default:throw(e);};};}` return `temp.length==l?nil:this._replace(temp)` end |
#replace(other) ⇒ Object
call-seq:
ary.replace(other) -> ary
Replaces the contents of ary with the contents of other, truncating or expanding if necessary.
a = %w(a b c)
a.replace(%w(w x y z)) #=> ["w", "x", "y", "z"]
a #=> ["w", "x", "y", "z"]
2559 2560 2561 |
# File 'lib/source/ruby.rb', line 2559 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]
2573 2574 2575 2576 |
# File 'lib/source/ruby.rb', line 2573 def reverse `for(var i=this.length,result=[];i>0;){result.push(this[--i]);}` return `result` end |
#reverse! ⇒ Object
call-seq:
ary.reverse! -> ary
Returns ary with its elements in reverse order.
a = [1,2,3,4,5]
a.reverse! #=> [5, 4, 3, 2, 1]
a #=> [5, 4, 3, 2, 1]
2588 2589 2590 |
# File 'lib/source/ruby.rb', line 2588 def reverse! `this.reverse()` 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
2606 2607 2608 2609 |
# File 'lib/source/ruby.rb', line 2606 def reverse_each `for(var i=this.length;i>0;){try{#{yield `this[--i]`};}catch(e){switch(e.__keyword__){case 'next':break;case 'break':return e.__return__;break;case 'redo':++i;break;default:throw(e);};};}` return self end |
#rindex(obj) ⇒ Object
call-seq:
ary.rindex(obj) -> integer or nil
Returns the highest index such that ary[index] == obj
, or nil
if obj is not found in ary.
a = %w(a b b b c)
a.rindex('b') #=> 3
a.rindex('z') #=> nil
2622 2623 2624 2625 |
# File 'lib/source/ruby.rb', line 2622 def rindex(obj) `for(var i=this.length;i>0;){if(this[--i].m$_eql2(obj)){return i;};}` return nil end |
#select ⇒ Object
2635 2636 2637 2638 |
# File 'lib/source/ruby.rb', line 2635 def select `for(var i=0,l=this.length,result=[];i<l;++i){try{if($T(#{yield `this[i]`})){result.push(this[i]);};}catch(e){switch(e.__keyword__){case 'next':if($T(e.__return__)){result.push(this[i]);};break;case 'break':return e.__return__;break;case 'redo':--i;break;default:throw(e);};};}` return `result` end |
#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"]
2651 2652 2653 2654 |
# File 'lib/source/ruby.rb', line 2651 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
2664 2665 2666 |
# File 'lib/source/ruby.rb', line 2664 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”
2695 2696 2697 |
# File 'lib/source/ruby.rb', line 2695 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"]
2717 2718 2719 2720 2721 2722 2723 2724 |
# File 'lib/source/ruby.rb', line 2717 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
2741 2742 2743 |
# File 'lib/source/ruby.rb', line 2741 def sort(block) `c$Array.apply(null,this)._quick_sort(0,this.length,block)` end |
#sort!(block) ⇒ Object
call-seq:
ary.sort! -> ary
ary.sort! { |a,b| block } -> ary
Returns ary with its elements sorted either by the <=>
operator or by the optional block, which should compare a and b and return -1, 0, or 1. See also Enumerable#sort_by
.
a = [3, 2, 4, 1]
a.sort! #=> [1, 2, 3, 4]
a.sort! {|a,b| b <=> a } #=> [4, 3, 2, 1]
a #=> [4, 3, 2, 1]
FIX: Doesn’t handle loop control keywords
2760 2761 2762 |
# File 'lib/source/ruby.rb', line 2760 def sort!(block) `this._quick_sort(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]
2772 2773 2774 2775 |
# File 'lib/source/ruby.rb', line 2772 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]
2784 2785 2786 |
# File 'lib/source/ruby.rb', line 2784 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"
2795 2796 2797 |
# File 'lib/source/ruby.rb', line 2795 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]]
2807 2808 2809 2810 2811 2812 2813 |
# File 'lib/source/ruby.rb', line 2807 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"]
2822 2823 2824 2825 |
# File 'lib/source/ruby.rb', line 2822 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"]
2839 2840 2841 2842 |
# File 'lib/source/ruby.rb', line 2839 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"]
2855 2856 2857 2858 |
# File 'lib/source/ruby.rb', line 2855 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
2862 2863 |
# File 'lib/source/ruby.rb', line 2862 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]
1755 1756 1757 1758 1759 |
# File 'lib/source/ruby.rb', line 1755 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 |