What's the Difference Between Array#size and Array#length in Ruby?

In Ruby, Array#size is an alias for Array#length. Beyond the name, they have no differences, and can be used interchangeably. They both return the number of elements in an array.

For example, you can get the length of an array with either one, like so:

puts [1, 2, 3].length #=> 3
puts [1, 2, 3].size #=> 3

For empty arrays, either method would return 0:

puts [].length #=> 0
puts [].size #=> 0

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.