Home » Ruby programming

Array.select! Method with Example in Ruby

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

Array.select! Method

In this article, we will study about Array.select! 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 select some elements from the Array. 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 not work if you do not specify any condition inside the block. Though it will not throw an exception you will get nil as the result. So, if you are working with this method you should be having a certain condition based on which the element is going to be selected from the object of Array class. If you want to print all the elements of Array instance then you can go for Array.each method and avoid going for this one.

Syntax:

    array.select!{|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.select! method
=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.select!{|num|
		num%2 == 0
	}
	puts "Array instance: #{num}"
elsif opt == 'b'
	puts "Odd numbers are:"
	puts num.select!{|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:
nter '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.select! 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. You can see that this method is creating a direct impact on the self Array instance because this method is one of the examples if destructive methods.

Example 2:

=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 num.select!{|a|}

Output

No Output.

Explanation:

In the above output, you can observe that when you are not specifying any condition inside the method, then you are not getting anything as the output.




Comments and Discussions!

Load comments ↻






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