Difference between #length, #size, and #count Array Methods in Ruby

Kevin Grow
3 min readOct 18, 2020

Ruby enthusiasts of all backgrounds have likely come across at least one of Ruby’s three array methods for determining the number of elements in an array. For those wondering why Ruby needs three distinct methods for this task, this guide will explain the differences (or lack thereof) between them.

The short answer is: #length and #size are array methods that can be used interchangeably while #count is an enumerable with a couple extra capabilities.

Syntax:

arr = [1,2,3,4,5]arr.length   # => 5
arr.size # => 5
arr.count # => 5
#extra count syntax:

arr = [1,2,3,3,4,5]
arr.count 3 # => 2
arr.count { |i| i < 4} # => 4

Let’s examine each method individually.

#length

The #length method will calculate the number of elements in an array. If the array has 10 elements within, using #length will simply return #=>10.

For example:

arr = [1,2,3,4,5]

arr.length # => 5

The length method simply counts how many elements are in the array which can be very useful for all sorts of different operations, such as finding an average of all the numbers in the array (the sum total of the numbers divided by their #length).

#size

The size method is an alias of #length, meaning that the two share their source code and can be used interchangeably. For more info on aliases, click here.

For example:

arr = [1,2,3,4,5] arr.length # => 5
arr.size # => 5
giant_saying = ["fee","fi","fo","fum"]giant_saying.length # => 4
giant_saying.size # => 4

As you can see, size and length produce the exact same outputs. Though #length and #size are generally used with arrays, they can also be used with strings in the same way.

For example

"foo".length # => 3

In this case, #length (or #size) will treat the string as an array and return the number of characters in the string, including spaces.

#count

Count, on the other hand, has features unavailable to #size and #length depending on the arguments added to it.

Example 1

With no arguments, #count will perform the same function as #size and #length.

arr = [1,2,3,4,5] 

arr.count # => 5

Interestingly, #count can not be used with a string in exactly the same way as #length and #size. For example:

"my name is jeff".count # => ArgumentError (wrong number of arguments (given 0, expected 1+))

In this case, #count is being used as a string method and requires an argument. A quick trip to the ruby methods documentation on strings reveals that the count requires a string arguments. It will then return the number of times that the string argument shows up in the original string. So for example:

"Mississippi".count("i") # => 4

In this case, the string “i” appears in “Mississippi” 4 times.

Example 2

As it turns out, count can do a similar job in arrays with a slightly different syntax. If an argument is added, #count will look for how many instances of that argument appear in the array. For example:

arr = [1,2,3,3,4,5]

arr.count(3) #=> 2

In this instance, since we added an argument, #count will return the number of times an element appears in the array. Since our argument (the integer 3) appears twice in the above array, #count will return the integer 2. Also, in terms of syntax, the parentheses are optional, for example:

arr = [1,2,3,3,4,5]arr.count 3  #=> 2

Example 3

Finally, count has a third function in which it will iterate through the array and return the number of elements in array that meet a certain condition. For example, it could return the number of elements greater than a particular number, or the number of elements within a range. For example:

arr = [1,2,3,3,4,5]  

arr.count { |i| i < 4} #=> 4

This syntax uses #count’s full power as an enumerable to let you seek out more complex data. If #count finds whatever meets to conditional you put in the code block, it will add it to the running total output after the code runs.

Conclusion

In conclusion, #length, #size, and #count perform extremely similar functions. #length and #size will both return only the number of elements in an array, while #count is able to perform a much wider range of options depending on the arguments passed into it. Now go and use your newfound counting powers for good!

--

--