Ruby Array Methods [D - E]

Techie     June 2022

1 . difference()

Returns a new Array containing only those elements that are not found in the arrays passed in the argument.

# Array.difference(other_array)

arr = [1, 2, 3, 4, 5]

arr.difference([1, 5])

# [2, 3, 4]


2 . delete_at()

Deletes an element from array, per the given index.

# Array.delete_at(index)

arr = [1, 2, 3, 4, 5]

arr.delete_at(2)

arr
# [1, 2, 4, 5]


3 . delete_if

Removes each element in array for which the block returns a truthy value.

# Array.delete_if {...}

arr = [1, 2, 3, 4, 5]

arr.delete_if {|a| a==4}

# [1, 2, 3, 5 ]


4 . drop()

Returns a new Array containing all but the first n element of array, where n is a non-negative Integer. Does not modify array.

# Array.drop(n)

arr = [1, 2, 3, 4, 5, [6, 8, 9], [8, 9, 10], 0]

arr.drop(5)

# [[6, 8, 9], [8, 9, 10], 0]


5 . drop_while

Calls the block with each successive element of array; stops if the block returns false or nil; returns a new Array omitting those elements for which the block returned a truthy value.

# Array.drop_while {...}

arr = [1, 2, 3, 4, 5]

arr.drop_while {|a| a > 3}

# [1, 2, 3]


6 . delete()

Removes the specified element from the array.

# Array.delete(element)

arr = [1, 2, 3, 4, 5]

arr.delete(3)

arr

# [1, 2, 4, 5]


7 . dig()

Returns the element/array in nested arrays that is specified by index and identifiers.

# Array.dig(index)

arr = [1, 2, [[6, 8, 9], [8, 9, 10]], 0]

arr.dig(2)

# [[6, 8, 9], [8, 9, 10]]


arr.dig(2, 1)

# [8, 9, 10]


8 . detect

Returns the first element for which the block returns a truthy value.

# Array.detect {...}

arr = [1, 2, 3, 4, 5]

arr.detect {|a| a > 2}

#  3


9 . dup

Produces a shallow copy of object. The instance variables of object are copied, but not the objects they reference. It also doesn’t copy the frozen value state of object unlike the clone method.

# Array.dup

arr = [4, 7, 1, 2, 3, 4, 5, 6, 7, 8, 9].freeze

arr.frozen?

# true

arr.object_id

# 5597800


dup_copy = arr.dup

dup_copy.frozen?

# false


dup_copy.object_id

# 5621360


# In comparisson to dup

clone_copy = arr.clone

clone_copy.frozen?

# true


clone_copy.object_id

# 5609580


10 . each_index

Passes each successive array index to the block.

# Array.each_index {...}

arr = [3, 4, "Ruby", "Rails"]

arr.each_index {|index|  puts "#{index} #{a[index]}" }

# 0 3
# 1 4
# 2 Ruby
# 3 Rails


11 . empty?

Returns true if the count of elements in array is zero, false otherwise.

# Array.empty?

arr = [1, 2, 3, 4, 5]

arr.empty?

# false


arr2 = [ ]

arr2.empty?

# true


12 . eql?

Returns true if array and another array are the same size and contain the same elements in the same index.

# Array.eql?(other_array)

arr = [1, 2, 3, 4]

arr2 = [1, 2, 3]

arr3 = [1, 2, 3, 4]


arr.eql?(arr2)

# false


arr.eql?(arr3)

# true


13 . each

Iterates over array elements. When a block is given, it passes each successive array element to the block and returns the array.

# Array.each

arr = [1, false, 3, "Ruby", true]

arr.each {|a| puts "#{a} - #{a.class}" }

# 1 - Integer
# false - FalseClass
# 3 - Integer
# Ruby - String
# true - TrueClass
# => [1, 2, 3, "Ruby", true]


14 . entries

This method is an alias for to_a method. Returns an array containing the items in self.

# Array.entries

arr = [1, 2, 3, 4]

arr.entries

# [1, 2, 3, 4]


(1..5).entries

# [1, 2, 3, 4, 5]


15 . each_with_index

With a block given, it calls the block with each element and its index and returns the array.

# Array.each_with_index {...}

arr = [3, 4, "Ruby", "Rails"]

arr.each_with_index {|a, i| p "#{i}. #{a}"}

# "0. 3"
# "1. 4"                                                                                                    
# "2. Ruby"                                                                                                 
# "3. Rails"                                                                            
# => [3, 4, "Ruby", "Rails"]                                                            


16 . each_entry

Calls the given block with each element, converting multiple values from yield to an array; returns self.

# Array.chunk

arr = []
(1..4).each_entry {|element| arr.push(element) }

arr

# [1, 2, 3, 4]


17 . each_slice

Iterates for each range of N elements and prints them. It takes an integer argument (slice size) into which the elements are grouped.

# Array.each_slice(slice_size) {...}

arr = [4, 7, 1, 2, 3, 4, 5, 6, 7, 8, 9]

arr.each_slice(4){|a| p a}

# [4, 7, 1, 2]
# [3, 4, 5, 6]
# [7, 8, 9]


18 . each_cons

Iterates for consecutive N elements starting from each element every time.

# Array.each_cons(size)

arr = [4, 7, 1, 2, 3, 4, 5, 6, 7, 8, 9]

arr.each_cons(5){|a| p a}

#  [4, 7, 1, 2, 3]
# [7, 1, 2, 3, 4]
# [1, 2, 3, 4, 5]
# [2, 3, 4, 5, 6]
# [3, 4, 5, 6, 7]
# [4, 5, 6, 7, 8]
# [5, 6, 7, 8, 9]


19 . each_with_object

Calls the block once for each element, passing both the element and the given object.

# Array.each_with_object(object) {...}

arr = [1, 2, 3, 4]

# e.g squaring the elements  
arr.each_with_object([]) {|i, a| a.push(i**2) }

# [1, 4, 9, 16]


20 . equal?

Checks for equality at the Object level and returns true only if array and another array are the same object.

# Array.equal?(another_array)

arr = [1, 2, 3, 4]

arr2 = arr.dup

arr3 = arr

arr.equal?(arr2)
# false

arr.equal?(arr3)
# true


Thanks for reading, see you in the next one!