find the length of an array in ruby

To find the length of an array in Ruby, you can use any of the three methods: length, size, or count. Here's an example of how to use each method:

main.rb
# Define an array
my_array = [1, 2, 3, 4, 5]

# Method 1: length
puts my_array.length   # Output: 5

# Method 2: size
puts my_array.size     # Output: 5

# Method 3: count
puts my_array.count    # Output: 5
207 chars
12 lines

All three methods will return the same result, which is the number of elements in the array.

related categories

gistlibby LogSnag