TExcelTAD
Home › Ruby › Ruby Array Methods

Ruby Array Methods

Arrays have dozens of built-in methods. This lesson groups the ones you will use most: sorting, searching, transforming, slicing and reshaping. As with strings, methods ending in ! change the array in place; the rest return a new array.

Sorting

ruby · try it yourself
scores = [72, 95, 58, 88, 95]
p scores.sort
p scores.sort.reverse
p scores.sort { |a, b| b <=> a }
p scores.max, scores.min
p scores.max(2)
p scores.minmax

names = %w[kiwi Banana apple Cherry]
p names.sort
p names.sort_by(&:downcase)
p names.sort_by { |n| [n.length, n] }
[58, 72, 88, 95, 95]
[95, 95, 88, 72, 58]
[95, 95, 88, 72, 58]
95
58
[95, 95]
[58, 95]
["Banana", "Cherry", "apple", "kiwi"]
["apple", "Banana", "Cherry", "kiwi"]
["kiwi", "apple", "Banana", "Cherry"]

Plain sort puts capital letters before lowercase ones (character codes). sort_by sorts by the value the block returns — sorting by an array like [length, name] gives you a tie-breaker.

Searching and testing

ruby · try it yourself
orders = [120, 45, 300, 80, 45]
p orders.find { |o| o > 100 }        # first match
p orders.find_index { |o| o > 100 }
p orders.select { |o| o < 100 }      # all matches
p orders.any? { |o| o > 250 }
p orders.all?(Integer)
p orders.none?(&:zero?)
p orders.count { |o| o >= 80 }
p orders.include?(300)
120
0
[45, 80, 45]
true
true
true
3
true

Transforming

ruby · try it yourself
words = ["sun", "moon", "star"]
p words.map(&:upcase)
p words.map.with_index { |w, i| "#{i}:#{w}" }
p words.each_with_object({}) { |w, h| h[w] = w.length }
p words.flat_map(&:chars).uniq.sort.join
p [1, 2, 3].sum { |n| n * n }
p [[1, 2], [3, 4]].to_h
p words.zip([3, 4, 4])
["SUN", "MOON", "STAR"]
["0:sun", "1:moon", "2:star"]
{"sun" => 3, "moon" => 4, "star" => 4}
"amnorstu"
14
{1 => 2, 3 => 4}
[["sun", 3], ["moon", 4], ["star", 4]]

Reducing

reduce (alias inject) combines all elements into one value:

ruby · try it yourself
p [1, 2, 3, 4].reduce(:+)
p [1, 2, 3, 4].reduce(1) { |product, n| product * n }
p %w[a b c].reduce { |acc, s| s + acc }
p [3, 9, 2].sum
p [[:x, 1], [:y, 2]].sum { |_, v| v }
10
24
"cba"
14
3

Slicing and grouping

ruby · try it yourself
nums = (1..10).to_a
p nums.take(3)
p nums.drop(7)
p nums.take_while { |n| n < 4 }
p nums.each_slice(4).to_a
p nums.partition(&:even?)
p nums.group_by { |n| n % 3 }
p nums.chunk_while { |a, b| b == a + 1 }.to_a.length
[1, 2, 3]
[8, 9, 10]
[1, 2, 3]
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10]]
[[2, 4, 6, 8, 10], [1, 3, 5, 7, 9]]
{1 => [1, 4, 7, 10], 2 => [2, 5, 8], 0 => [3, 6, 9]}
1

Cleaning up

ruby · try it yourself
messy = [3, nil, 1, 3, [2, [nil, 4]], 1]
p messy.compact
p messy.flatten
p messy.flatten.compact.uniq
p messy.flatten.compact.tally
p [5, 3, 5, 1].uniq.sort
[3, 1, 3, [2, [nil, 4]], 1]
[3, nil, 1, 3, 2, nil, 4, 1]
[3, 1, 2, 4]
{3 => 2, 1 => 2, 2 => 1, 4 => 1}
[1, 3, 5]

Bang methods modify in place

ruby · try it yourself
list = [4, 2, 4, nil, 1]
list.compact!
list.uniq!
list.sort!
p list

list.map! { |n| n * 10 }
list.select!(&:positive?)
p list

p [1, 2].sort!       # bang methods may return nil when nothing changed:
p [1, 2].uniq!
[1, 2, 4]
[10, 20, 40]
[1, 2]
nil

Note: Some bang methods such as uniq!, compact! and flatten! return nil when they made no change. Don't chain them — call them on their own line.

Converting to strings

ruby · try it yourself
parts = ["2025", "07", "14"]
p parts.join("-")
p parts.join
p [1, [2, 3]].join(",")
p parts.inspect
puts "Items: #{parts}"
"2025-07-14"
"20250714"
"1,2,3"
"[\"2025\", \"07\", \"14\"]"
Items: ["2025", "07", "14"]

Cheat sheet

Goal Method
Sort sort, sort_by, reverse, max, min, minmax
Find find, select/filter, reject, find_index, include?
Test any?, all?, none?, one?, count
Transform map, flat_map, zip, each_with_object, to_h
Combine reduce/inject, sum, join
Slice take, drop, take_while, drop_while, each_slice, first, last
Group group_by, partition, tally, chunk_while
Clean compact, uniq, flatten
Random sample, shuffle (not deterministic!)

Test yourself

What does [3, 1, 2].sort_by { |n| -n } return?

  1. [1, 2, 3]
  2. [3, 2, 1]
  3. [-3, -2, -1]
  4. [3, 1, 2]

What is [1, 2, 3, 4].partition(&:odd?)?

  1. [[1, 3], [2, 4]]
  2. [[2, 4], [1, 3]]
  3. [1, 3]
  4. {true => [1, 3], false => [2, 4]}

What does [1, 2].uniq! return?

  1. [1, 2]
  2. nil
  3. true
  4. An error