Ruby arrays are ordered, integer-indexed collections of any objects. They double as stacks, queues, and function as the basis for most data processing in Ruby.
| Name | Signature | Description |
|---|---|---|
| push / << | arr.push(obj) or arr << obj → arr | Append element |
| pop | arr.pop → obj | Remove and return last element |
| shift | arr.shift → obj | Remove and return first element |
| unshift | arr.unshift(obj) → arr | Prepend element |
| first/last | arr.first(n) / arr.last(n) | First/last n elements |
| length/size | arr.length → Integer | Number of elements |
| empty? | arr.empty? → bool | True if array has no elements |
| include? | arr.include?(obj) → bool | True if array contains obj |
| flatten | arr.flatten(depth) → Array | Return flattened copy |
| compact | arr.compact → Array | Return copy without nil values |
| uniq | arr.uniq → Array | Return copy with duplicates removed |
| sort | arr.sort → Array | Return sorted copy |
| sort_by | arr.sort_by {|e| key} → Array | Sort by computed key |
| reverse | arr.reverse → Array | Return reversed copy |
| map/collect | arr.map {|e| expr} → Array | Transform each element |
| select/filter | arr.select {|e| bool} → Array | Keep matching elements |
| reject | arr.reject {|e| bool} → Array | Remove matching elements |
| reduce/inject | arr.reduce(init) {|acc,e| expr} | Fold to single value |
| each | arr.each {|e| block} → arr | Iterate over elements |
| each_with_index | arr.each_with_index {|e,i| block} | Iterate with index |
| flat_map | arr.flat_map {|e| Array} → Array | Map then flatten one level |
| zip | arr.zip(other) → Array | Zip with another array |
| sample | arr.sample(n) → obj/Array | Return random element(s) |
| shuffle | arr.shuffle → Array | Return randomly shuffled copy |
| min/max | arr.min / arr.max → obj | Minimum/maximum element |
| sum | arr.sum → Number | Sum all elements |
| count | arr.count(obj) → Integer | Count occurrences |
| tally | arr.tally → Hash | Count occurrences of each element |
| any? | arr.any? {|e| bool} → bool | True if any element matches |
| all? | arr.all? {|e| bool} → bool | True if all elements match |
| none? | arr.none? {|e| bool} → bool | True if no elements match |
| group_by | arr.group_by {|e| key} → Hash | Group all elements by key |
| filter_map | arr.filter_map {|e| expr_or_nil} | Map and filter nils (Ruby 2.7+) |
| combination | arr.combination(n) → Enumerator | All n-element combinations |
| permutation | arr.permutation(n) → Enumerator | All n-element permutations |
| intersection & | arr & other → Array | Elements common to both |
| union | | arr | other → Array | All unique elements from both |
| difference - | arr - other → Array | Elements in arr not in other |
| rotate | arr.rotate(n) → Array | Rotate elements left by n |
| each_slice | arr.each_slice(n){|chunk| block} | Iterate in chunks of n |
| each_cons | arr.each_cons(n){|window| block} | Sliding window of n |
Run with ruby script.rb.
# Array — Ruby's workhorse
nums = [5, 3, 8, 1, 9, 2, 7, 4, 6]
puts nums.sort.inspect
puts nums.select(&:odd?).inspect
puts nums.map { |n| n**2 }.inspect
puts nums.reduce(:+)
puts nums.min_by { |n| (n - 5).abs }
# Chaining
words = %w[banana apple cherry date elderberry fig]
result = words
.select { |w| w.length > 4 }
.map(&:capitalize)
.sort
puts result.inspect
# tally
votes = %w[go ruby rust go java go ruby rust go]
puts votes.tally.sort_by { |_,v| -v }.inspect
# filter_map (Ruby 2.7+)
puts (1..10).filter_map { |n| n**2 if n.odd? }.inspect
# zip
names = %w[Alice Bob Carol]
scores = [95, 87, 92]
names.zip(scores).each { |n,s| puts "#{n}: #{s}" }
# each_slice — process in batches
words.each_slice(2) { |batch| puts batch.inspect }
# Combinations
puts [1,2,3].combination(2).to_a.inspect
# ruby array_demo.rb