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

Ruby Array#size vs. Array#length

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

Hope you found this post useful. It was published . Please show your love and support by sharing this post.