Array Methods
Part of the Fundamentals section of Coddy's Ruby journey — lesson 61 of 88.
Ruby provides several built-in methods that make working with arrays easier. These methods help you sort, reverse, and find elements without writing complex code yourself.
The sort method arranges elements in ascending order:
numbers = [3, 1, 4, 1, 5]
puts numbers.sort # Outputs: 1, 1, 3, 4, 5To reverse the order of elements, use the reverse method:
letters = ["a", "b", "c"]
puts letters.reverse # Outputs: c, b, aYou can combine these methods to sort in descending order:
scores = [85, 92, 78, 95]
puts scores.sort.reverse # Outputs: 95, 92, 85, 78The min and max methods quickly find the smallest and largest values:
prices = [29, 15, 42, 8]
puts prices.min # Outputs: 8
puts prices.max # Outputs: 42To calculate the total of numeric elements, use sum:
quantities = [2, 5, 3]
puts quantities.sum # Outputs: 10Note that sort and reverse return new arrays without changing the original. To modify the original array directly, add ! to the method name, like sort! or reverse!.
Challenge
EasyRead a single line of input containing comma-separated integers (e.g., 5,2,8,1,9,3).
Split the input into an array of integers, then print the following on separate lines:
- The array sorted in ascending order (each element on its own line)
- The array sorted in descending order (each element on its own line)
- The minimum value
- The maximum value
- The sum of all elements
Print a blank line between each section for clarity.
For example, if the input is 5,2,8,1,9,3, the output should be:
1
2
3
5
8
9
9
8
5
3
2
1
1
9
28If the input is 10,30,20, the output should be:
10
20
30
30
20
10
10
30
60If the input is 7,3,5,1, the output should be:
1
3
5
7
7
5
3
1
1
7
16Cheat sheet
Ruby provides built-in methods for working with arrays:
The sort method arranges elements in ascending order:
numbers = [3, 1, 4, 1, 5]
puts numbers.sort # Outputs: 1, 1, 3, 4, 5The reverse method reverses the order of elements:
letters = ["a", "b", "c"]
puts letters.reverse # Outputs: c, b, aCombine methods to sort in descending order:
scores = [85, 92, 78, 95]
puts scores.sort.reverse # Outputs: 95, 92, 85, 78The min and max methods find the smallest and largest values:
prices = [29, 15, 42, 8]
puts prices.min # Outputs: 8
puts prices.max # Outputs: 42The sum method calculates the total of numeric elements:
quantities = [2, 5, 3]
puts quantities.sum # Outputs: 10Note: sort and reverse return new arrays without changing the original. To modify the original array, use sort! or reverse!.
Try it yourself
# Read the comma-separated integers
input = gets.chomp
# TODO: Write your code below
# 1. Split the input into an array of integers
# 2. Sort the array in ascending order and print each element
# 3. Print a blank line
# 4. Sort the array in descending order and print each element
# 5. Print a blank line
# 6. Print the minimum value
# 7. Print the maximum value
# 8. Print the sum of all elementsThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 42Variables and Data Types
Numbers and VariablesString Data TypeBoolean Data TypeSymbol Data TypeChecking Data TypesNaming ConventionsRecap - Variable Creation8Loops
For Loop with RangesWhile LoopBreakNextRecap - FactorialTimes LoopUntil LoopNested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators6Basic IO
Output with putsOutput with print and pOutput With VariablesInput with getsChomp MethodType ConversionRecap - Age CalculatorRecap - True or False