To get the length of an array in Ruby, you can use the length
method.
In this example,
numbers
with some values.length
method to get the length of the array and store the returned value in length
variable.length
to standard output.numbers = [1, 2, 3, 4, 5]
length = numbers.length
puts "Length of array is: #{length}"
Length of array is: 5
In this example,
names
with some strings.length
method to get the length of the array and store the returned value in length
variable.length
to standard output.names = ["Alice", "Bob", "Charlie"]
length = names.length
puts "Length of array is: #{length}"
Length of array is: 3
In this tutorial, we learned How to Get Array Length in Ruby language with well detailed examples.