From: Robert Klemme Date: 2011-05-03T03:30:40+09:00 Subject: Re: Help making a method more concise On 02.05.2011 20:17, Mark Hayes wrote: > I'm looking to improve my skills as a Rubyist and would like to know if the > "depth" method could be expressed more precisely. Any help would be greatly > appreciated, thanks! > > require 'test/unit' > > class Node > attr_accessor :value, :lchild, :rchild > > def depth > [lchild ? lchild.depth : 0, rchild ? rchild.depth : 0].max + 1 > end > end I think it's pretty OK that way. You could get rid of a bit of redundancy but whether that actually makes the program better? Judge for yourself: def depth [lchild, rchild].map {|ch| ch ? ch.depth : 0}.max + 1 end def depth d = 0 [lchild, rchild].each do |ch| d = ch.depth if ch && ch.depth > d end d + 1 end def depth d = 0 [lchild, rchild].each do |ch| d = [d, ch.depth].max if ch end d + 1 end Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/