find the length of a map in ruby

To find the length of a map in Ruby, you can use the length, size, or count method. These methods are synonymous and can be used interchangeably. Here's an example:

main.rb
my_map = { "a" => 1, "b" => 2, "c" => 3 }
puts my_map.length #=> 3
puts my_map.size   #=> 3
puts my_map.count  #=> 3
117 chars
5 lines

All three methods return the same result, which is the number of key-value pairs in the map. In the example above, the map has 3 key-value pairs, so each of the methods returns 3.

related categories

gistlibby LogSnag