Thanks Robert, I think I'll go with this one:

def depth
 [lchild, rchild].map {|ch| ch ? ch.depth : 0}.max + 1
end


On Mon, May 2, 2011 at 11:30 AM, Robert Klemme
<shortcutter / googlemail.com>wrote:

> 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/
>
>


-- 


Mark Hayes
mark / deployfx.com
<mark / deployfx.com>