• BlockByte
  • Posts
  • Ruby Arrays: A Beginner's Guide

Ruby Arrays: A Beginner's Guide

Navigating the World of Ruby Arrays: Tips, Tricks, and Techniques

Creating an Array

Let's explore how to create arrays in Ruby using different approaches:

# You can create an array in several ways in Ruby:

---------

# 1) Creates an empty array
my_array = Array.new

# 2) Creates an array with 3 nil elements
my_array = Array.new(3)

# 3) Creates an array with 3 elements, each set to "default"
my_array = Array.new(3, "default")

Output for 1: []
Output for 2: [nil, nil, nil]
Output for 3: ["default", "default", "default"]

---------

# You can also create an array directly with real values
my_array = [1, 'two', 3.0]
Output: [1, 'two', 3.0]

---------

Accessing Elements

Elements in an array can be accessed using their index. Ruby also supports negative indices, which count backward from the end of the array.

my_array = [10, 20, 30, 40, 50]

my_array [0] # => 10, access the first element
my_array [2] # => 30, access the third element
my_array [-1] # => 50, access the last element
my_array [-3] # => 30, access the third-to-last element

Modifying Elements

Modifying an array's elements can be done by directly assigning a new value to a chosen index:

my_array = [10, 20, 30]
my_array [1] = "changed"

print my_array
Output: [10, "changed", 30]

Adding and Removing Elements

Ruby arrays offer various methods to add or remove elements, allowing for dynamic adjustments to the array's contents:

my_array = [10, 20, 30]



---------
# Adding elements at the end
arr.push(40)

print my_array
Output: [10, 20, 30, 40]
---------
# Adding elements at the end
my_array << 40

print my_array
Output: [10, 20, 30, 40]
---------
# Adding elements at the beginning
my_array.unshift(123) 

print my_array
Output: [123, 10, 20, 30]
---------


---------
# Removes the last element
my_array.pop

print my_array
Output: [10, 20]
---------
# Removes the first element
my_array.shift

print my_array
Output: [20, 30]
---------

Iterating Over Arrays (each)

You can iterate over arrays using various methods, like each.

arr = [10, 20, 30]

arr.each do |element|
  puts element
end

Output:
10
20
30
  • arr = [10, 20, 30]: Initializes an array with elements 10, 20, and 30.

  • arr.each do |element|: Iterates over each element of the array arr.

  • puts element: Prints the current element to the console.

  • end: Ends the iteration block.

Selecting Elements (select)

You can select elements based on conditions using the select method.

arr = [1, 2, 3, 4, 5]
even_numbers = arr.select { |num| num.even? } 

Output:
[2, 4]
  • arr = [1, 2, 3, 4, 5]: Initializes an array with elements 1 through 5.

  • even_numbers = arr.select { |num| num.even? }: Selects even numbers from arr.

    • arr.select: Iterates over each element of arr.

    • |num| num.even?: For each element (num), checks if it is even using even? method.

    • Returns a new array containing only the elements for which the block returns true.

Transforming Elements (map)

The map method allows you to transform each element of an array and return a new array with the transformed elements.

arr = [1, 2, 3, 4, 5]
squared_numbers = arr.map { |num| num ** 2 }

Output:
[1, 4, 9, 16, 25]
  • arr = [1, 2, 3, 4, 5]: Initializes an array with elements 1 through 5.

  • squared_numbers = arr.map { |num| num ** 2 }: Applies a block to each element of arr, squaring each number.

    • arr.map: Iterates over each element of arr.

    • |num| num ** 2: For each element (num), calculates its square (num ** 2).

    • Returns a new array with the squared values.