Ruby array.length Code Example

Last Updated On Tuesday 25th Jan 2022
  • An array is an ordered collection of objects that can be thought of as a list
  • length() is a method that returns the number of elements in the array.
  • You can check how many elements an array has with the length method.

Example 1

	fruits = ['apple', 'orange', 'grape', 'banana']
print(fruits.length) # 4
	

Example 2

	mixed = [5, "Hello World", "FRR", true, [1, 2, 3]]

print(mixed.length) # 5
	

Example 3

	array = ["Spring", "Summer", "Fall", "Winter"]
print(array.length) # 4