Dear Rubyist,

I want to count array list [1,2,3,[4],5] —> # 5 leaves

but I always get sum_of_array.

Where did I do wrong?

def array_leave1 # sum_of_array?

res = 0

self.each{|elem|

if elem.kind_of?(Array)

res += elem.sum_of_array

else

res += elem

end

}

return res

end

def number_of_leaves

self.inject(0){|acc,x|

if x.kind_of?(Array)

acc += x.number_of_leaves

else

acc += x

end

}

end

many thanks in advance.

regards,

salai