Menu
Coddy logo textTech

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, 5

To reverse the order of elements, use the reverse method:

letters = ["a", "b", "c"]
puts letters.reverse  # Outputs: c, b, a

You can combine these methods to sort in descending order:

scores = [85, 92, 78, 95]
puts scores.sort.reverse  # Outputs: 95, 92, 85, 78

The 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: 42

To calculate the total of numeric elements, use sum:

quantities = [2, 5, 3]
puts quantities.sum  # Outputs: 10

Note 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 icon

Challenge

Easy

Read 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:

  1. The array sorted in ascending order (each element on its own line)
  2. The array sorted in descending order (each element on its own line)
  3. The minimum value
  4. The maximum value
  5. 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
28

If the input is 10,30,20, the output should be:

10
20
30

30
20
10

10
30
60

If the input is 7,3,5,1, the output should be:

1
3
5
7

7
5
3
1

1
7
16

Cheat 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, 5

The reverse method reverses the order of elements:

letters = ["a", "b", "c"]
puts letters.reverse  # Outputs: c, b, a

Combine methods to sort in descending order:

scores = [85, 92, 78, 95]
puts scores.sort.reverse  # Outputs: 95, 92, 85, 78

The min and max methods find the smallest and largest values:

prices = [29, 15, 42, 8]
puts prices.min  # Outputs: 8
puts prices.max  # Outputs: 42

The sum method calculates the total of numeric elements:

quantities = [2, 5, 3]
puts quantities.sum  # Outputs: 10

Note: 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 elements
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals