From: Mark Hayes Date: 2011-05-03T03:17:05+09:00 Subject: Help making a method more concise --bcaec54306faa23c5904a24f074a Content-Type: text/plain; charset=ISO-8859-1 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 -- Mark mark@deployfx.com --bcaec54306faa23c5904a24f074a--