Ruby 3 · Basics

Transform arrays

Arrays have rich methods, and most take blocks.

numbers = [1, 2, 3, 4, 5]
squares = numbers.map { |n| n * n }
evens = numbers.select(&:even?)
total = numbers.sum

Official Ruby 3 documentation

More basics commands

All basics commands · Ruby 3 cheatsheet

Was this page helpful?