Home » Ruby programming

Array.collect Method with Example in Ruby

Ruby Array.collect Method: Here, we are going to learn about the Array.collect method with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on January 07, 2020

Ruby Array.collect Method

In previous articles, we have seen how we can implement Array.assoc() and Array.include?() methods in our Ruby code. Both the methods were used for finding the presence of certain objects in an object of Array class. Both the methods had certain differences and we have gone through them as well. In the article, we will see how we can use Array.collect method in the Ruby program. We will implement the method with the help of its syntax and program codes.

Method description:

This method comes under the category of public instance method and is defined for the Array class in the library of Ruby language. This method traverses through the Array instance and creates modifications according to the requirement of the Ruby code. This method can be destructive as well as non-destructive which means that if the method is destructive then the modifications done by this method will be reflected in the actual Array instance, you can make the method destructive by adding a "!" mark after the method. As discussed above, this method can be non-destructive as well which simply means that the changes created by this method would not affect the actual Array instance.

Syntax:

    array_instance.collect {#block}

Example 1:

=begin
	Ruby program to demonstrate collect method
=end

# array declaration
array1 = ["1","Ramesh","Apple","12","Sana","Yogita","Satyam","Harish"]

# input
puts "Enter the character you want to add"
ele = gets.chomp

puts "Array instance after modification:"
print array1.collect { |x| x + ele } 

puts ""

puts "Actual Array:"
puts array1     

Output

Enter the character you want to add
 ^
Array instance after modification:
["1^", "Ramesh^", "Apple^", "12^", "Sana^", "Yogita^", "Satyam^", "Harish^"]
Actual Array:
1
Ramesh
Apple
12
Sana
Yogita
Satyam
Harish

Explanation:

In the above code, you can observe that this variant of the Array.collect method is a non-destructive one as the changes made by the method cannot be seen in the actual Array instance when we are printing it with the help of puts statement.

Example 2:

=begin
	Ruby program to demonstrate collect! method
=end

# array declaration
array1 = ["1","Ramesh","Apple","12","Sana","Yogita","Satyam","Harish"]

# input
puts "Enter the character you want to add"
ele = gets.chomp

puts "Array instance after modification:"
print array1.collect! { |x| x + ele } 

puts ""

puts "Actual Array:"
puts array1        

Output

Enter the character you want to add
 %
Array instance after modification:
["1%", "Ramesh%", "Apple%", "12%", "Sana%", "Yogita%", "Satyam%", "Harish%"]
Actual Array:
1%
Ramesh%
Apple%
12%
Sana%
Yogita%
Satyam%
Harish%

Explanation:

In the above code, you can observe that this variant of Array.collect method is a destructive one as the changes made by the method can be seen in the actual Array. You can see that in both the statements we are getting an additional "%" symbol with all the elements present in the Array instance. This example simply tells you how you can create the method destructive with the help of "!" symbol.






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.