Arrays in Ruby | Exploring Essential Array Methods in Ruby

 


Array

In Ruby arrays, ordered collection of objects. They can hold any combination of objects including integers, strings, symbols, and even other arrays. Here's how arrays work in Ruby: 

Creating an array

Array can be created using the `[]` or `Array.new` method.
  • using square braces
        arr1 = [1, 2, 3] 
  • using Array.new
        arr2 = Array.new(3) # Creates an array with 3 nil elements
              

Access the array of elements

In Ruby, array elements can be accessed by their index, starting from 0. We can use the `[]` or the `slice` method.

  • using square braces
        arr1 = [1, 2, 3]
        p arr1[0] # output: 1
        p arr1[0..2] # output: [1, 2, 3]           

  • using the `slice` method
        arr1 = [1, 2, 3, 4, 5]
        p arr1.slice(1)
        p arr1.slice(1, 3)

Useful Ruby Methods

  • Map: This method will perform the operation on an element, and return the new array.

            arr1 = [1, 2, 3, 4, 5]
            arr2 = arr1.map { |ele| ele + 3 }
            p arr2 # output: [4, 5, 6, 7, 8]

  • Select: This will filter out the elements from the array, based on the block condition and return the new array
            arr1 = [1, 2, 3, 4, 5]
            arr2 = arr1.select { |ele| ele > 2 }
            p arr2 # output: [3, 4, 5]

  • Reject: It is similar to the `select` method, unlike `select` it will return the array of elements that does not meet the block condition.
            arr1 = [1, 2, 3, 4, 5]
            arr2 = arr1.reject { |ele| ele > 2 }
            p arr2 # output: [1, 2]

  • Inject: The `reduce` method functions similarly to `inject`, combining all array elements and applying the specified binary operation through the provided block. In fact, `reduce` is an alias for the `inject` method.
            arr1 = [1, 2, 3, 4, 5]
            sum = arr1.inject(0) { |ele, num| ele + num }
            p sum # output: 15

  • Group By: It is used to group an element such as an array, based on the result of a block.
            arr1 = [1, 2, 3, 4, 5]
            h = arr1.group_by {|x| x.even? ? "even" : "odd"}
            p h


  • Partition: It is used to split the elements of an array into two groups, based on the results of a block. It returns an array containing two sub-arrays, one for elements that satisfy the condition and the other for those that do not.

            arr1 = [1, 2, 3, 4, 5]
            arr2 = arr1.partition {|x| x.even? }
            p arr2 # output: [[2, 4], [1, 3, 5]]


  • Keep if:  This method operates similarly to the `select`, but instead of producing a new array, it modifies the original array in place.

            arr1 = [1, 2, 3, 4, 5]
            arr1.keep_if {|x| x.even? }
            p arr1 # output: [2, 4]

  • delete if:  This method operates similarly to the `reject`, but instead of producing a new array, it modifies the original array in place.
            arr1 = [1, 2, 3, 4, 5]
            arr1.delete_if {|x| x.even? }
            p arr1 # output: [1, 3, 5]


Ruby provides a wide range of methods to manipulate arrays. Some commonly used methods include:

  • size or length: Returns the number of elements in the array.
  • empty?: Returns true if the array contains no elements.
  • include?(obj): Returns true if the array contains obj.
  • sort: Returns a new array sorted.
  • reverse: Returns a new array with elements in reverse order.
  • uniq: Returns a new array with duplicate elements removed.
  • compact: Returns a new array with nil elements removed.

Arrays are fundamental data structures in Ruby, offering flexibility and versatility in storing and manipulating collections of data.

Comments