Array Methods
In this page:
Array Methods
Ruby arrays have dozens of useful built-in methods beyond the basics, like .sort, .uniq, .include?, .sum, .min, .max, and .first/.last. Most of these come from the Enumerable module that Array includes, so they're also available on other collection types like Hash and Range. Learning these built-ins is key to writing concise, idiomatic Ruby instead of manual loops.
Note: Because Array includes Enumerable, methods like sum, min, and sort also work on any custom class that includes Enumerable.
Example: Array Methods
numbers = [5, 3, 8, 1, 9, 3]
puts numbers.sort.inspect
puts numbers.uniq.inspect
puts numbers.include?(8)
puts numbers.sum
puts numbers.max
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: