Home » Ruby programming

Array.reject! Method with Example in Ruby

Ruby Array.reject! Method: Here, we are going to learn about the Array.reject! Method with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on February 12, 2020

Array.reject! Method

In this article, we will study about Array.reject! Method. You all must be thinking the method must be doing something related to the selection of objects from the Array instance. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.

Method description:

This method is a public instance method and defined for the Array class in Ruby's library. This method, as the name suggests, is used to reject some elements from the Array. The elements will not be printed if they are satisfying the condition or criteria provided inside the block. This method is destructive and brings changes in the actual values of the Array object. This method works based on a certain condition which you will provide inside the pair of parenthesis. This method is based on the criteria you provide inside the block. This method will work even if you do not specify any condition inside the block. It will print all the values if you are not providing any condition or criteria of rejection.

Syntax:

    array.reject!{|var| #condition}

Argument(s) required:

This method does not permit the passing of any arguments instead it mandates a condition.

Example 1:

=begin
  Ruby program to demonstrate Array.reject!
=end

# array declaration
num = [2,44,2,5,7,83,5,67,12,11,90,78,9]

puts "Enter 'a' for Even numbers and 'b' for odd numbers"
opt = gets.chomp

if opt == 'a'
	puts "Even numbers are:"
	puts num.reject!{|num|
		num%2 != 0
	}
	puts "Array instance: #{num}"
elsif opt == 'b'
	puts "Odd numbers are:"
	puts num.reject!{|num|
		num%2 ==0
	}
	puts "Array instance: #{num}"
else
	puts "Wrong selection. Input valid option"
end

Output

RUN 1:
Enter 'a' for Even numbers and 'b' for odd numbers
 a
Even numbers are:
2
44
2
12
90
78
Array instance: [2, 44, 2, 12, 90, 78]

RUN 2:
Enter 'a' for Even numbers and 'b' for odd numbers
 b
Odd numbers are:
5
7
83
5
67
11
9
Array instance: [5, 7, 83, 5, 67, 11, 9]

Explanation:

In the above code, you can observe that we are taking input from the user about what type of numbers the user wants as the output. This is because we want to pass certain conditions inside the Array.reject! method. We are giving responses to the user as per the option provided by the user and this method is used in this way only. It is rejecting the elements which are satisfying the condition provided inside the block. You can see that this method is creating a direct impact on the self Array instance because this method is one of the examples of destructive methods.

Example 2:

=begin
  Ruby program to demonstrate Array.reject!
=end

# array declaration
num = [2,44,2,5,7,83,5,67,12,11,90,78,9]

puts num.reject!{|a|}

Output

No Output.

Explanation:

In the above output, you can observe that when you are not specifying any condition inside the method, it is still not throwing any kind of exception inside it will print all the elements present in the Array instance. This is one more point by which we can differentiate Array.select! and Array.reject!.



Comments and Discussions!

Load comments ↻





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