Home » Ruby programming

Array.each Method with Example in Ruby

Ruby Array.each method: Here, we are going to learn about the Array.each method in Ruby programming language which is used to iterate over the loop.
Submitted by Hrithik Chandra Prasad, on December 21, 2019

Ruby Array.each method

Array.each method can be easily termed as a method which helps you to iterate over the Array. This method first processes the first element then goes on the second and the process keeps on going on until the last element is not processed. This method is very important when it comes to carrying out the process of traversing the array. This method works in the way that each element of the instance of Array class is yielded to the block supplied to the method. The task is completed in a particular sequence. You will need to provide a temporary variable inside the pair of the disjoint symbol because you will be able to fetch the element with the help of that variable only. This simply means that first the value of an element is temporarily stored in that variable and it is stored ever since the next element does not come into the scene.

This operation or process does not bring any change in the actual values of Array elements.

Syntax:

    Array.each { |var| #statements}

Parameter(s):

This method does not invite any type of argument.

Example 1:

=begin
Ruby program to demonstrate Array.each
=end

# array declaration
Adc = ['Ruby','Includehelp.com','Ruby','C++','C#']

# counter intialization
cnt = 1

# Array.each method
Adc.each{ |ele|
	puts "#{cnt} element is #{ele}"
	cnt = cnt + 1
}

Output

1 element is Ruby
2 element is Includehelp.com
3 element is Ruby
4 element is C++
5 element is C#

The above code can be modified as,

Example 2:

=begin
Ruby program to demonstrate Array.each
=end

# array declaration
Adc = ['Ruby','Includehelp.com','Ruby','C++','C#']

# counter initialization
cnt = 1
Adc.each do |ele|
	puts "#{cnt} element is #{ele}"
	cnt = cnt + 1
end

Output

1 element is Ruby
2 element is Includehelp.com
3 element is Ruby
4 element is C++
5 element is C#

Explanation:

In the above two program codes, you can observe that the method named Array.each can be used in two different ways. Both the ways are quite understandable and their use depends upon your comfort. It is very clear with the help of output that the processing of Array elements starts from index 0 and finishes when the last element is being processed. You can never bring any change in the elements of Array with the help of this method because this method is one of the examples of non-destructive methods.






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.