DEV Community

Shay
Shay

Posted on

Ruby: Array Class

The Array class is a fundamental data structure used to store and manipulate collections of objects. Arrays in Ruby are ordered, indexed collections of objects, meaning the elements are arranged in a specific order and can be accessed using their position, or index, within the array. The Array class provides a wide range of methods for adding, removing, accessing, and manipulating elements within an array.

Here's an overview of the Array class in Ruby:

Creating Arrays

You can create arrays in Ruby using literal notation or constructor methods.

Literal Notation: Enclosing comma-separated values within square brackets ([]).

my_array = [1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Using Constructor: Calling the Array.new constructor method and passing initial values as arguments.

my_array = Array.new(3, "hello")
Enter fullscreen mode Exit fullscreen mode

Accessing Elements

You can access elements within an array using square brackets ([]) notation, specifying the index of the element you want to access.

my_array = [10, 20, 30, 40, 50]
puts my_array[0] # Output: 10
Enter fullscreen mode Exit fullscreen mode

Array Methods

The Array class provides a variety of methods for working with arrays. Some common methods include:

Adding and Removing Elements
Push: Add elements to the end or beginning of an array.
Pop: Remove elements from the end or beginning of an array.

Accessing and Modifying Elements
[], []=: Access and modify elements at specific indices.
at, fetch: Access elements by index, with bounds checking.
slice, slice!: Extract or remove a portion of an array.

Iterating over Elements
each, map, select: Iterate over elements in the array.
each_with_index: Iterate over elements along with their indices.
cycle: Repeatedly iterate over elements in the array.

Searching and Sorting
include?, index: Check if an element exists in the array and find its index.
sort, reverse, shuffle: Sort, reverse, or shuffle the elements in the array.
min, max: Find the minimum or maximum element in the array.

Array Properties
Size: You can determine the size of an array using the size or length methods.
Empty?: Check if an array is empty using the empty? method.
Equality: Compare arrays for equality using the == operator.

Multi-dimensional Arrays

Ruby's Array class also supports multi-dimensional arrays, allowing you to create arrays of arrays.

multi_array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
puts multi_array[1][2] # Output: 6
Enter fullscreen mode Exit fullscreen mode

Iterating Over Multi-dimensional Arrays
You can iterate over multi-dimensional arrays using nested loops or methods like each.

multi_array.each do |row|
  row.each do |element|
    puts element
  end
end
Enter fullscreen mode Exit fullscreen mode

Using Array.new Constructor

multi_array = Array.new(3) { Array.new(3, 0) }
Enter fullscreen mode Exit fullscreen mode

Using Nested Loops

size = 3
multi_array = []
size.times do
  inner_array = []
  size.times do
    inner_array << 0
  end
  multi_array << inner_array
end
Enter fullscreen mode Exit fullscreen mode

Happy coding!

theGlamTechie

Top comments (0)