From: Josh Cheek Date: 2011-05-03T04:55:30+09:00 Subject: Re: Help making a method more concise --90e6ba53ac2ec40b8404a25067a9 Content-Type: text/plain; charset=ISO-8859-1 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. --90e6ba53ac2ec40b8404a25067a9--