Chautauqua Logo

Ruby class Array interactive

Prompt icon → Click to get an interactive sample (type in code)
! → Has also a bang method (modifies self)
(  ) → Optional argument

Creating

# arr = [1,'b',:dig]
%warr = %w[a b c] # ['a','b','c'] Documentation Interactive samplePlayground
[nil, nil, nil]

Methods

Creating an Array

[]Array[1, 'a', :dig] # [1,'a',:dig] Documentation Interactive samplePlayground
[1, "a", :dig]
newArray.new([1, 'b', :dig]) # [1,'b',:dig] Documentation Interactive samplePlayground
[1, "b", :dig]
new(count)Array.new(2) # [nil,nil] Documentation Interactive samplePlayground
[nil, nil]
new(count,val)Array.new(2, 'a') # ['a','a'] Documentation Interactive samplePlayground
["a", "a"]
newArray.new(2) {|i| "E#{i}" } # ['E0','E1'] Documentation Interactive samplePlayground
["E0", "E1", "E3"]
try_convertArray.try_convert([1, 2]) # [1,2] or nil Documentation Interactive samplePlayground
[1, 2, 3]; nil

Querying

all?[1, 1, 1].all?(1) # true Documentation Interactive samplePlayground
true
any?[1, 2, 3].any?(2) # true Documentation Interactive samplePlayground
true
count[1, 2, 3].count # 3 Documentation Interactive samplePlayground
3
empty?[1, 2, 3].empty? # false Documentation Interactive samplePlayground
false
index (alias find_index)['a', 'b', 'c', 'c'].index('c') # 2 Documentation Interactive samplePlayground
2
rindex['a', 'b', 'c', 'c'].rindex('c') # 3 Documentation Interactive samplePlayground
3
hash['a', 'b', 'c'].hash # 6345789 (differs) Documentation Interactive samplePlayground
include?[1, 2, 3].include?(2) # true Documentation Interactive samplePlayground
true
none?[1, 2, 3].none?(7) # true Documentation Interactive samplePlayground
true
one?(count)[1, 2, 3].one?(2) # true Documentation Interactive samplePlayground
true
size (alias length)[1, 2, 3].size # 3 Documentation Interactive samplePlayground
3

Comparing

<=>[0, 1, 2] <=> [0, 1, 2] # 0 (-1,0,1) Documentation Interactive samplePlayground
0
==[0, 1, 2] == [0, 1, 2] # true Documentation Interactive samplePlayground
true
eql?[0, 1, 2].eql?([0, 1, 2]) # true Documentation Interactive samplePlayground
true

Fetching

[] (alias slice)!arr[1] or arr.slice(1) # b Documentation Interactive samplePlayground
1
assoc[1, 'a', [5,6]].assoc(5) # [5,6] Documentation Interactive samplePlayground
[5, 6]
rassoc[1, 'a', [5,6]].rassoc(6) # [5,6] Documentation Interactive samplePlayground
[5, 12, 7]
atarr.at(0) # 1 Documentation Interactive samplePlayground
1
bsearch[0, 6, 7].bsearch {|x| x >= 4 } # 6 Documentation Interactive samplePlayground
6
bsearch_index[0, 6, 7].bsearch_index {|x| x >= 4 } # 1 Documentation Interactive samplePlayground
1
compact![nil, 2, 2, nil, 3].compact # [2,2,3] Documentation Interactive samplePlayground
[2, 2, 3]
dig['a',2,[1,[3,4]]].dig(2, 1) # [3,4] Documentation Interactive samplePlayground
[3, 4]
droparr.drop(1) # ['b',:dig] Documentation Interactive samplePlayground
["b", :dig]
drop_while[0, 1, 6, 7].drop_while {|x| x<=4} # [6,7] Documentation Interactive samplePlayground
[6, 7]
fetcharr.fetch(2) # :dig Documentation Interactive samplePlayground
:dig
fetch_valuesarr.fetch_values(2,0) # [:dig,1] Documentation Interactive samplePlayground
[:dig, 1]
first(count)arr.first(2) # [1,'b'] Documentation Interactive samplePlayground
[1, "b"]
last(count)arr.last(2) # ['b',:dig] Documentation Interactive samplePlayground
["b", :dig]
max(count)[2,0,3,1].max # 3 Documentation Interactive samplePlayground
3
min(count)[2,0,3,1].min # 3 Documentation Interactive samplePlayground
3
minmax[2,0,3,1].minmax # [0,3] Documentation Interactive samplePlayground
[0, 3]
reject!arr.reject {|e| e.to_s[0]=='b'} # [1,:dig] Documentation Interactive samplePlayground
[1, :dig]
reverse!arr.reverse # [:dig,'b',1] Documentation Interactive samplePlayground
[:dig, "b", 1]
rotate(count)!arr.rotate(1) # ['b',:dig,1] Documentation Interactive samplePlayground
["b", :dig, 1]
sample(count)arr.sample # (random) Documentation Interactive samplePlayground
[random, random]
select!arr.select {|e| e.to_s[0]=='b'} # b Documentation Interactive samplePlayground
["b", :big, "bugger"]
shuffle!arr.shuffle # [random,random,random] Documentation Interactive samplePlayground
[random, random, random]
sort![3, 0, 1, 2].sort # [0,1,2,3] Documentation Interactive samplePlayground
[0, 1, 2, 3]
take[0,1,2,3,4].take(3) # [0,1,2] Documentation Interactive samplePlayground
[0, 1, 2]
take_while[1,2,3,4].take_while {|e| e < 3 } # [1,2] Documentation Interactive samplePlayground
[1, 2]
uniq![1,1,2,3,3].uniq # [1,2,3] Documentation Interactive samplePlayground
[1, 2, 3]
values_at!arr.values_at(2,0) # [:dig,1] Documentation Interactive samplePlayground
[:dig, 1]

Assigning

<<arr << 'dog; # [1,'a',:dig,'dog'] Documentation Interactive samplePlayground
[1, "a", :dig, "dog"]
[]=arr[2] = 'dog' # [1,'a','dog'] Documentation Interactive samplePlayground
[1, "a", "dog"]
concatarr.concat([3, :cat]) # [1,'b',:dig,3,:cat] Documentation Interactive samplePlayground
[1, "b", :dig, "dog", :cat]
fillarr.fill('-', 1, 2) # [1,'-','-'] Documentation Interactive samplePlayground
[1, "-", "-"]
replace .initialize_copyarr.replace(['fox','cat']) # ['cat','rat'] Documentation Interactive samplePlayground
["fox", "cat", "bee"]
insertarr.insert(1,:bee,3) # [1,:bee,3,'b',:dig] Documentation Interactive samplePlayground
[1, :bee, 3, "b", :dig]
append (alias push)arr.append(:bee,3) # [1,'b',:dig,:bee,3] Documentation Interactive samplePlayground
[1, "b", :dig, :bee, 3]
prepend (alias unshift)arr.prepend(:bee,3) # [:bee,3,1,'b',:dig] Documentation Interactive samplePlayground
[:bee, 3, 1, "b", :dig]
sort_by!arr.sort_by! {|e| e.size } # ['b',:dig,1] Documentation Interactive samplePlayground
["b", :dig, 1]
! See non-bang methods for:
flatten! reverse! rotate! shuffle! sort!

Deleting

cleararr.clear # [] Documentation Interactive samplePlayground
[]
deletearr.delete('b') # [1,:dig] Documentation Interactive samplePlayground
"b"; [1, :dig]
delete_ifarr.delete_if {|e| e.to_s[0]=='d'} # [1,'b'] Documentation Interactive samplePlayground
[1, "b"]
keep_ifarr.keep_if {|e| e.to_s[0]=='d'} # [:dig] Documentation Interactive samplePlayground
[1, "b"]
pop(count)arr.pop # :dig Documentation Interactive samplePlayground
["b", :dig]
shift(count)arr.shift # 1 Documentation Interactive samplePlayground
[1, "b"]; [:dig]
! See non-bang methods for:
compact! reject! select!→(alias filter!) slice!→(see []) uniq!

Combining

&[0,1,2,3] & [1,2] # [1,2] Documentation Interactive samplePlayground
[1, 2]
+[0,1] + [2,3] # [0,1,2,3] Documentation Interactive samplePlayground
[0, 1, 2, 3]
-[1,1,1,2,3] - [1] # [2,3] Documentation Interactive samplePlayground
[2, 3]
|[1,1,2] | [3,3,4] # [1,2,3,4] Documentation Interactive samplePlayground
[1, 2, 3, 4]
difference[1,1,2,3,1].difference([1]) # [2,3] Documentation Interactive samplePlayground
[2, 3]
intersection[0,1,2].intersection([2,1]) # [1,2] Documentation Interactive samplePlayground
[1, 2]
product[1].product([2,3,7]) # [[1,2],[1,3],[1,7]] Documentation Interactive samplePlayground
[[0, 2], [0, 3], [1, 2], [1, 3]]
union[0,1,1].union([2,1],[3,1]) # [1,2] Documentation Interactive samplePlayground
[0, 1, 2, 3]

Iterating

combination[1,2,3].combination(2){|c|p c} # [1,2][1,3]… Documentation Interactive samplePlayground
[1, 2, 3]; [[1, 2], [1, 3], [2, 3]]
cycle(count|FOREVER)[0,1].cycle(2){|c|p c} # 0;1;0;1 Documentation Interactive samplePlayground
nil; [1, 2, 3, 1, 2, 3]
each[1,2,3].each{|c|p c} # 1;2;3 Documentation Interactive samplePlayground
["a", "b", "c"]; abc
each_indexarr.each_index{|i|p i} # 0;1;2 Documentation Interactive samplePlayground
["a", "b", "c"]; 0-a;1-b;2-c;
permutation[0,1].permutation(2){|c|p c} # [0,1];[1.0] Documentation Interactive samplePlayground
[0, 1, 2]; [0, 1][0, 2][1, 0][1, 2][2, 0][2, 1]
repeated_combination[0,1].repeated_combination(1){…} # [0,0]… Documentation Interactive samplePlayground
[0, 1, 2]; [0, 0][0, 1][0, 2][1, 1][1, 2][2, 2]
repeated_permutation[0,1].repeated_permutation(1){…} # [0,0]… Documentation Interactive samplePlayground
[0, 1, 2]; [0, 0][0, 1][0, 2][1, 0][1, 1][1, 2][2, 0][2, 1][2, 2]
reverse_each[0,1].reverse_each(1){|c|p c} # [0];[1] Documentation Interactive samplePlayground
[0, 1, 2]; [0, 0][0, 1][0, 2][1, 1][1, 2][2, 2]

Converting

map (alias collect)arr.map{|e| e.class} # [Int…,String,Symbol] Documentation Interactive samplePlayground
[Integer, String, Symbol]
flatten[0, [1,2]].flatten # [0,1,2] Documentation Interactive samplePlayground
[0, 1, 2, 3, 4, 5]
to_s (alias inspect)arr.to_s # '[1,\"b\",:dig]' Documentation Interactive samplePlayground
"[1, \"b\", :dig]"
joinarr.join('|') # 1|b|dig Documentation Interactive samplePlayground
1|b|dig
to_aa=MyA.new([1,2]);a.to_a (.class) # MyA;Array Documentation Interactive samplePlayground
MyArr; Array
to_arya=MyA.new([1,2]);a.to_ary (.class) # MyA;MyA Documentation Interactive samplePlayground
MyArr; MyArr
to_h[['a',0],['b',1]].to_h # {'a'=>0,'b'=>1} Documentation Interactive samplePlayground
{"a" => 0, "b" => 1}
transpose[['a',0],…].transpose # [['a','b'],[0,1]] Documentation Interactive samplePlayground
[["a", "b"], [0, 1]]
zip[0].zip(['a'],[:c]) # [[0,'a',:c]] Documentation Interactive samplePlayground
[[0, "a", :c], [1, "b", :d]]
! See non-bang methods for:
map!→(alias collect!)

Other Methods

*['a','b'] * 2 # ['a','b','a','b'] Documentation Interactive samplePlayground
["a", "b", "a", "b"]
pack[33,42,38].pack('CCC') # '!*&' Documentation Interactive samplePlayground
!*&
sum(number)[1,2,3].sum # 6 Documentation Interactive samplePlayground
16