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.


    array.select!{|var| #condition}

Argument(s) required:

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

Example 1:

  Ruby program to demonstrate Array.select! method

# 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}"
	puts "Wrong selection. Input valid option"


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

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


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:

  Ruby program to demonstrate Array.select!

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

puts num.select!{|a|}


No Output.


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

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.