Home » Ruby programming

Array.delete_if Method with Example in Ruby

Ruby Array.delete_if Method: In this tutorial, we are going to learn about the Array.delete_if method with syntax, examples.
Submitted by Hrithik Chandra Prasad, on December 25, 2019

Ruby Array.delete_if Method

In the last articles, we have studied the Array methods namely Array.select, Array.reject and Array.drop_While, all these methods are non–destructive methods which means that they do not impose any changes in the actual values of elements residing in the Array instance. If you want to make the above method destructive, you can add an "!" after the method name. For instance, Array.select! is the destructive version of Array.select.

In this article, we will learn about the Array method Array.delete_if which is already destructive by nature.

Method description:

The changes created by this method are always permanent as it is one of a kind of destructive methods. This method works in a way that if it finds an element which is not satisfying the Boolean condition which is specified inside the block of the method, then it deletes that element from the Array instance. It will not delete any element if it does not find the Boolean condition specified inside the block of the method.

Syntax:

    Array.delete_if{|var|#condition}

Parameter (s): This method does not accept any arguments instead it requires a Boolean condition for operation.

Example 1:

=begin
    Ruby program to demonstrate Array.delete_if
=end

# array declaration
num = [1,2,3,4,5,6,7,8,9,10,23,11,33,55,66,12]

# input
puts "Enter the your choice (a)delete odd numbers (b) delete even numbers"
lm = gets.chomp

if lm == 'a'
    puts "Even numbers are:"
    puts num.delete_if { |a| a % 2 !=0 }
elsif lm == 'b'
    puts "Odd numbers are:"
    puts num.delete_if { |a| a % 2 ==0 }
else
	puts "Invalid Input"
end

Output

RUN 1:
Enter the your choice (a)delete odd numbers (b) delete even numbers
a
Even numbers are:
2
4
6
8
10
66
12

RUN 2:
Enter the your choice (a)delete odd numbers (b) delete even numbers
b
Odd numbers are:
1
3
5
7
9
23
11
33
55

Explanation:

In the above code, you can observe that the method is deleting all the elements which are satisfying the condition provided inside the block of Array.delete_if method. If the user is asking to delete odd numbers, then output is shown in RUN2 and when the user is asking for even numbers, the output is shown in the RUN1.

Example 2:

=begin
    Ruby program to demonstrate Array.delete_if
=end
# array declaration
num = [1,2,3,4,5,6,7,8,9,10,23,11,33,55,66,12]

print num.delete_if{|a|}
puts ""
print num

Output

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 23, 11, 33, 55, 66, 12]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 23, 11, 33, 55, 66, 12]

Explanation:

In the above code, you can observe that if you are not specifying any condition inside the method block, then it is not deleting or removing any element from the Array instance.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.