Home »
Ruby Tutorial
Different methods to find the length of an Array in Ruby
By IncludeHelp Last updated : November 22, 2024
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. Finding array length using Array.each method
Syntax:
Array.each do |element|
end
Example
=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. Finding array length using 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
Example
=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. Finding array length using 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
Example
=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. Finding array length using 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
Example
=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