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 example in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on December 22, 2019

Ruby Array.reject Method

In the last article, we have seen how we can make use of the Array.select method in order to print the Array elements based on certain conditions provided inside the block? In this article, we will see how we can make use of the Array.reject method? Array.reject method is totally opposite of the Array.select method.

Array.reject 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 a non-destructive method and does not bring any change in the actual values of the Array object. This method works on the basis of certain conditions which you will provide inside the pair of parentheses. This method is totally based on the criteria you provide inside the block. This method will work even if you do not specify any conditions 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}

Parameter(s):

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

Example 1:

=begin
  Ruby program to demonstrate Array.select
=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 == 'b'
  puts "Odd numbers are:"
  puts num.reject{|num|
    num%2 == 0
  }
elsif opt == 'a'
  puts "Even numbers are:"
  puts num.reject{|num|
    num%2 !=0
  }
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

RUN 2:
Enter 'a' for Even numbers and 'b' for odd numbers
 b
Odd numbers are:
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 the response 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.

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

2
44
2
5
7
83
5
67
12
11
90
78
9

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

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.