Home » Ruby programming

Different methods to find the length of an Array in Ruby

Length of the array in Ruby: Here, we are going to learn about the various methods to find the length of an Array in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on December 16, 2019

If you are reading an article that is related to obtaining information about the instance of Array class then it is expected from you that you are aware of the basic things related to Array, such as how to declare it?

In this article, you will come to know about various methods that are available in Ruby's library specifically defined to obtain the length of the Array. We are very well aware of the fact that Ruby's arrays are dynamic which means that we can store n number of elements in them without taking care or bothering about their predefined size. So, you will need to check the number of elements present in the Array at a particular point of time while working with Ruby Arrays. We have got multiple methods which are defined in Ruby's library to serve this purpose, they are namely Array.count, Array.length and Array.count.

In the rest of the article, you will get to see the four ways through which you can obtain information about the number of elements present in the object of Array class.

1) Array.each method

Syntax:

    Array.each do |element|
    end

Program:

=begin
Ruby program to find length using Array.each method	
=end

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

# counting length
cont = 0
Adc.each do |ele|
	cont=cont+1
end

# printing the lenght
puts "Number of elements present in Array are #{cont}"

Output

Number of elements present in Array are 6

Explanation:

In the above code, you must have observed that we have declared a variable named cont and using it inside the Array.each method. Every time, when the method is finding any element in the Array, the value of cont is increased by one.

2) Array.count method

We can find the number of elements present in the Array with the help of the Array.count method as well. Refer to the syntax and code given below.

Syntax:

    Array.count

Program:

=begin
Ruby program to find length using Array.count method	
=end

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

# counting array length
num = Adc.count

# printing the length
puts "Number of elements present in Array are #{num}"

Output

Number of elements present in Array are 6

3) Array.length method

We can find the number of elements present in the Array with the help of the Array.length method as well. Refer to the syntax and code given below.

Syntax:

    Array.length

Program:

=begin
Ruby program to find length using Array.length method	
=end

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

# counting the array length
num = Adc.length

# printing the array length
puts "Number of elements present in Array are #{num}"

Output

Number of elements present in Array are 6

4) Array.size method

We can find the number of elements present in the Array with the help of the Array.size method as well. Refer to the syntax and code given below.

Syntax:

    Array.size

Program:

=begin
Ruby program to find length using Array.size method	
=end

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

# counting array length
num = Adc.size

# printing the array length
puts "Number of elements present in Array are #{num}"

Output

Number of elements present in Array are 6


Comments and Discussions!

Load comments ↻





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