Arrays are Ruby’s most used collection type. I will use very little description as I give examples for the many different ways to work with Arrays. Different ways to create an empty Array
|
[] Array.new Array[] # same as :new Array(nil) # tries converting to an Array first and insures an Array result %w^^ # you can use any character pair-up after the w. content won't need quotes %W^^ # Allows string interpolation |
Filling an Array
|
laughing_pirate = Array.new(3, :arr) # => [:arr, :arr, :arr] [42] * 5 # => [42, 42, 42, 42, 42] [1,2,3].insert(10,8) # position, item ## fills beyond with nil # => [1, 2, 3, nil, nil, nil, nil, nil, nil, nil, 8] x = [1,2,3] x[9] = 10 # just like insert # => [1, 2, 3, nil, nil, nil, nil, nil, nil, 10] |
Insertion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
[1,2,3,4,5,6].insert 4, :hello # => [1, 2, 3, 4, :hello, 5, 6] x = [1,2,3,4,5,6,7,8] x[2,4] = :hello # slice replacement x # => [1, 2, :hello, 7, 8] x[2,2] = [3,4,5,6] # slice replacement x # => [1, 2, 3, 4, 5, 6, 8] x[2] = [8,7] # creates inner array x # => [1, 2, [8, 7], 4, 5, 6, 8] x[1..4] = [7] # range slice insertion x # => [1, 7, 6, 8] |
Boolean/Truthiness
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
[false, false, false].all? # => false [true, false, false].all? # => false [true, true, true].all? # => true [false, false, false].any? # => false [false, true, false].any? # => true [1,2,3,4].all?(&:even?) # => false [1,2,3,4].any?(&:even?) # => true [1,2,3,4].any? {|item| item > 2 && item < 4} # => true [1,2,3,4].one? # => false [4].one? # => true [1,2,3,4].one?(&:even?) # => false [1,2,3].one?(&:even?) # => true [1,2,3].empty? # => false [].empty? # => true [1,2,3].include? 5 # => false [1,2,3].frozen? # => false [1,2,3].eql? [1,2] # => false [1,2,3].== [1,2] # => false |
Transformations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
# zip ['H', 'l', 'o', 'o', 'l'].zip( ['e', 'l', 'W', 'r', 'd'] ) # => [["H", "e"], ["l", "l"], ["o", "W"], ["o", "r"], ["l", "d"]] # transpose [["H", "e"], ["l", "l"], ["o", "W"], ["o", "r"], ["l", "d"]].transpose # => [["H", "l", "o", "o", "l"], ["e", "l", "W", "r", "d"]] # product [:a,:b,:c].product [1,2,3] # => [[:a, 1], [:a, 2], [:a, 3], [:b, 1], [:b, 2], [:b, 3], [:c, 1], [:c, 2], [:c, 3]] # combination (no repetitions of pairings) [:a,:b,:c,:d].combination(2).to_a # => [[:a, :b], [:a, :c], [:a, :d], [:b, :c], [:b, :d], [:c, :d]] # permutation (will give both [a,b] and [b,a]) [:a,:b,:c].permutation(2).to_a # => [[:a, :b], [:a, :c], [:b, :a], [:b, :c], [:c, :a], [:c, :b]] # reverse and reverse! [1,2,3].reverse # => [3, 2, 1] # sort and sort! [2,1,3].sort # => [1, 2, 3] # shuffle and shuffle! [1,2,3].shuffle # => [3, 1, 2] # uniq and uniq! [2,1,3,3,1,2].uniq # => [2, 1, 3] # rotate and rotate! [:a,:b,:c].rotate # => [:b, :c, :a] # compact and compact! [:a,:b,nil,nil,nil,:c].compact # => [:a, :b, :c] # flatten and flatten! [["H", "e"], ["l", "l"], ["o", "W"], ["o", "r"], ["l", "d"]].flatten # => ["H", "e", "l", "l", "o", "W", "o", "r", "l", "d"] |
Slicing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
# slice and slice! [1,2,3,4,5,6,7,8].slice(2) # => 3 [1,2,3,4,5,6,7,8][2] # => 3 [1,2,3,4,5,6,7,8].slice(2..5) # => [3, 4, 5, 6] [1,2,3,4,5,6,7,8][2..5] # => [3, 4, 5, 6] [1,2,3,4,5,6,7,8].slice(2,5) # => [3, 4, 5, 6, 7] [1,2,3,4,5,6,7,8][2,5] # => [3, 4, 5, 6, 7] # Enumerables [8,7,6,5,4,3,2,1].slice_after(2).to_a # => [[8, 7, 6, 5, 4, 3, 2], [1]] [8,7,6,5,4,3,2,1].slice_before(2).to_a # => [[8, 7, 6, 5, 4, 3], [2, 1]] [1,2,4,9,10,11,12,15,16,19,20,21].slice_when {|i,j| i+1 != j}.to_a # => [[1, 2], [4], [9, 10, 11, 12], [15, 16], [19, 20, 21]] |
Adding
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
[2,3,4] + [5,6,7] # => [2, 3, 4, 5, 6, 7] [2,3,4] << [5,6,7] # => [2, 3, 4, [5, 6, 7]] [2,3,4] .concat [5,6,7] # => [2, 3, 4, 5, 6, 7] [2,3,4] .unshift [5,6,7] # => [[5, 6, 7], 2, 3, 4] [2,3,4] .push [5,6,7] # => [2, 3, 4, [5, 6, 7]] [2,3,4] | [3,4,5,6,7] # => [2, 3, 4, 5, 6, 7] |
Replace & Remove
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
[2,3,4] .replace [4,5,6] # => [4, 5, 6] x = [1,2,3,4,5] x.pop # => 5 x # => [1, 2, 3, 4] x.shift # => 1 x # => [2, 3, 4] x.clear # => [] x = [1,2,3,4,5] x.delete(2) # => 2 x # => [1, 3, 4, 5] x.delete_at(3) # => 5 x # => [1, 3, 4] [1,2,3,4,5,6,7,8].delete_if(&:even?) # => [1, 3, 5, 7] [1,2,3,4,5,6,7,8].delete_if {|n| n.even? && n > 4} # => [1, 2, 3, 4, 5, 7] [1,2,3,4,5,6,7,8].keep_if(&:odd?) # => [1, 3, 5, 7] [:a,:b,:c,:d,:e] - [:a,:e] # => [:b, :c, :d] |
Selecting
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
# Already covered are keep_if, delete_if, slice, [] [1,2,3,4,5,6,7,8].select(&:even?) # => [2, 4, 6, 8] [1,2,3,4,5,6,7,8].select {|n| n < 3 || n > 5} # => [1, 2, 6, 7, 8] [1,2,3,4,5,6,7,8].detect(&:even?) # Detect gets first truth result # => 2 [1,2,3,4,5,6,7,8].values_at(2) # => [3] [1,2,3,4,5,6,7,8].values_at(2,4) # => [3, 5] [1,2,3,4,5,6,7,8].values_at(2,4,5) # => [3, 5, 6] [1,2,3,4,5,6,7,8].values_at(2..5) # => [3, 4, 5, 6] # reject and reject! [1,2,3,4,5,6,7,8].reject(&:even?) # => [1, 3, 5, 7] [1,2,3,4,5,6,7,8].find {|i| i.even?} # => 2 [1,2,3,4,6,8,9,11,13,14,15,16].chunk(&:even?).to_a # => [[false, [1]], [true, [2]], [false, [3]], [true, [4, 6, 8]], [false, [9, 11, 13]], [true, [14]], [false, [15]], [true, [16]]] [1,2,3,4,5,6,7,8].take(3) # => [1, 2, 3] [1,2,3,4,5,6,7,8].group_by(&:even?) # => {false=>[1, 3, 5, 7], true=>[2, 4, 6, 8]} [2,3,4] .partition {|i| i.even?}.to_a # => [[2, 4], [3]] [0,1,2,3,4,5,6,7,9].group_by.with_index {|_,index| index % 4 }.values # => [[0, 4, 9], [1, 5], [2, 6], [3, 7]] |
Mapping…
Continue Reading »