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

class NodeTest < Test::Unit::TestCase

def setup

@n = Node.new

end

def test_depth_of_one

@n.lchild = Node.new

@n.lchild.lchild = Node.new

@n.rchild = Node.new

assert @n.depth == 1, “depth of node should be one, was #{@n.depth}”

end

def test_depth_of_two

@n.lchild = Node.new

@n.rchild = Node.new

assert @n.lchild.depth == 1, “depth of lchild should be one”

assert @n.rchild.depth == 1, “depth of rchild should be one”

assert @n.depth == 2, “depth of tree should be two, was #{@n.depth}”

end

end

–