Ruby Array Tutorial

1. Introduction

Arrays are fundamental data structures that store an ordered collection of items. In Ruby, arrays are flexible and can hold items of mixed data types. This tutorial covers various methods associated with arrays in Ruby.

An Array in Ruby is an ordered collection of objects, which can be of any type, including other arrays. The elements in an array can be accessed by their index, which starts from 0.

Common Methods:

1. push and <<: Add elements to the end.

2. pop: Removes the last element.

3. shift: Removes the first element.

4. unshift: Adds elements to the beginning.

5. include?: Checks if an element is present.

6. index: Finds the index of the first occurrence of an element.

7. select: Returns a new array with all elements for which the block returns true.

8. map: Returns a new array with the results of running block once for every element.

9. delete_at: Deletes element at a specific index.

10. length or size: Returns the number of elements.

11. empty?: Checks if the array is empty.

12. sort: Returns a new array with elements sorted.

13. reverse: Returns a new array with elements in reverse order.

2. Program Steps

1. Initialize an array.

2. Demonstrate the common methods on the initialized array.

3. Code Program

The below Ruby program demonstrates the common methods of an Array:
# Initialize an array
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
puts "Original Array: #{numbers}"

# 1. Add elements to the end.
numbers.push(8)
numbers << 0

# 2. Removes the last element.
numbers.pop

# 3. Removes the first element.
numbers.shift

# 4. Adds elements to the beginning.
numbers.unshift(7, 3)

# 5. Checks if an element is present.
has_four = numbers.include?(4)
# 6. Finds the index of the first occurrence of an element.

index_of_five = numbers.index(5)
# 7. Returns a new array with elements meeting the condition.
even_numbers = numbers.select { |num| num.even? }

# 8. Returns a new array with each element transformed.
squared_numbers = numbers.map { |num| num * num }

# 9. Deletes element at a specific index.
numbers.delete_at(4)

# 10. Returns the number of elements.
length = numbers.length

# 11. Checks if the array is empty.
is_empty = numbers.empty?

# 12. Returns a new array with elements sorted.
sorted_numbers = numbers.sort

# 13. Returns a new array with elements in reverse order.
reversed_numbers = numbers.reverse
puts "Updated Array: #{numbers}"
puts "Array includes 4: #{has_four}"
puts "Index of 5: #{index_of_five}"
puts "Even Numbers: #{even_numbers}"
puts "Squared Numbers: #{squared_numbers}"
puts "Length: #{length}"
puts "Is Empty: #{is_empty}"
puts "Sorted: #{sorted_numbers}"
puts "Reversed: #{reversed_numbers}"

Output:

Original Array: [3, 1, 4, 1, 5, 9, 2, 6]
Updated Array: [7, 3, 4, 1, 9, 2, 6, 8]
Array includes 4: true
Index of 5: nil
Even Numbers: [4, 2, 6, 8]
Squared Numbers: [49, 9, 16, 1, 81, 4, 36, 64]
Length: 8
Is Empty: false
Sorted: [1, 2, 3, 4, 6, 7, 8, 9]
Reversed: [8, 6, 2, 9, 1, 4, 3, 7]

Explanation:

1. We first initialize an array with some numbers.

2. The push and << methods allow us to add elements to the end of the array.

3. Using pop, we remove the last element and with shift, the first element.

4. unshift adds elements to the beginning of the array.

5. include? is a query method that checks for the presence of an element.

6. index returns the position of the first occurrence, or nil if not found.

7. select is used to filter the array based on a condition.

8. map transforms each element of the array.

9. delete_at removes an element at a specific position.

10-13. The final methods showcase properties and transformations like length, emptiness check, sorting, and reversing.


Comments