Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Ruby
Array Number of Leaves
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="salai, post: 4651107"] 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 [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Ruby
Array Number of Leaves
Top