From: Mark Hayes Date: 2011-05-03T06:10:38+09:00 Subject: Re: Help making a method more concise --bcaec53961c62e393304a25174bd Content-Type: text/plain; charset=ISO-8859-1 Hey Josh, Oops ... I see that my test case was incorrect. It should've been: def test_depth_of_one @n.lchild = Node.new @n.lchild.lchild = Node.new @n.rchild = Node.new assert @n.depth == 3, "depth of node should be one, was #{@n.depth}" end Thanks for bringing that to my attention! On Mon, May 2, 2011 at 12:55 PM, Josh Cheek wrote: > On Mon, May 2, 2011 at 1:17 PM, Mark Hayes 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 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 > > > > > > > First test fails, with a value of 3. > 3 seems like the correct value, your test may be wrong. > -- Mark Hayes mark@deployfx.com --bcaec53961c62e393304a25174bd--