EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Ruby

Collections

An array is a collection of elements, which can be of the same or different types, stored in contiguous memory locations. The concept behind arrays is to group multiple elements together under a single variable name. In Ruby, arrays can store various types of objects like numbers, strings, hashes, symbols, or even other arrays. Arrays in Ruby are indexed, meaning each element is associated with an index number, starting from 0 for positive indices. Negative indices start from -1, representing elements from the end of the array.

Example of an Array:

["Ruby", 42, 88.5, "Programming"]

In this example, the array contains four elements: a string ("Ruby"), an integer (42), a float (88.5), and another string ("Programming").

  • Positive indices start from 0.
  • Negative indices start from -1 and represent elements from the end of the array.

Ruby also supports multidimensional arrays (arrays within arrays), but here we will focus on one-dimensional arrays.

Creating a 1-D Array in Ruby

There are various ways to create an array in Ruby, but two of the most common methods are:

1. Using the new method: The new method can be used to create arrays by specifying its size and optional default values. The Array.new method can take zero, one, or more arguments.

Syntax:

array_name = Array.new

Example:

arr = Array.new

Here, arr is an empty array. To create an array with a specific size, pass a number to the new method.

Example:

arr = Array.new(10) # Creates an array of size 10
puts arr.size       # Output: 10
puts arr.length     # Output: 10

You can also specify a default value for each element.

Example:

arr = Array.new(3, "Hello")
puts "#{arr}"  # Output: ["Hello", "Hello", "Hello"]

Program Example:

# Ruby program demonstrating array creation using the new method
arr1 = Array.new()         # Empty array
arr2 = Array.new(5)        # Array with 5 nil elements
arr3 = Array.new(4, "Hi")  # Array of size 4 with default element "Hi"

puts arr1.size             # Output: 0
puts arr2.length           # Output: 5
puts arr3.size             # Output: 4
puts "#{arr3}"             # Output: ["Hi", "Hi", "Hi", "Hi"]

2. Using the literal constructor []The literal constructor [] can be used to quickly define an array with elements inside square brackets.

Example:

arr = ['x', 'y', 'z', 'a', 'b']
puts "#{arr}"              # Output: ["x", "y", "z", "a", "b"]
puts "Size of array: #{arr.size}"   # Output: 5

3. Accessing Elements from an Array: In Ruby, you can access elements of an array using indices. The most common way is to use the element’s index number in square brackets []. If you try to access an element that doesn’t exist, Ruby will return nil.

Example:

# Ruby program to demonstrate element access
arr = ["Hello", "World", "Ruby", "Array"]
puts arr[1]     # Output: World
puts arr[-1]    # Output: Array (last element)

4. Accessing Multiple Elements: To retrieve multiple elements from an array, pass two arguments to the [] method, specifying the starting index and the number of elements to retrieve.

Example:

# Ruby program to demonstrate accessing multiple elements
arr = ["Hello", "World", "Ruby", "Array"]
puts arr[1, 2]  # Output: ["World", "Ruby"]
End of lesson.