We can use the length
property to find the length of a Hash in Ruby.
hash.length
This method returns the count of entries in the hash
.
# create some hashes h1 = {one: 1, two: 2, three: 3} h2 = {name: "okwudili", stack: "ruby"} h3 = {"foo": 0, "bar": 1} h4 = {M:"Mango", A: "Apple", B: "Banana"} h5 = {} # get the length of # hashes and print results puts h1.length # 3 puts h2.length # 2 puts h3.length # 2 puts h4.length # 3 puts h5.length # 0
h1
,h2
,h3
,h4
, and h5
.hash
, using the length
property. Then, we print the results to the console.RELATED TAGS
CONTRIBUTOR
View all Courses