On Mon, May 2, 2011 at 11:17 AM, Mark Hayes <mark / deployfx.com> wrote:
> Hello,
>
> 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 begreatly
> 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


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

or even:

def depth
   1 + [lchild,rchild].map(&:to_i).max
end

alias to_i depth

or:

add empty (no value) left & right children when you first add a value
to a node, and:

def empty?
  @value.nil?
end

def depth
  empty? ? 0 : [lchild,rchild].map(&:depth).max
end