From: Mark Hayes Date: 2011-05-03T03:41:15+09:00 Subject: Re: Help making a method more concise --bcaec53961c63d964704a24f5e7f Content-Type: text/plain; charset=ISO-8859-1 Thanks Robert, I think I'll go with this one: def depth [lchild, rchild].map {|ch| ch ? ch.depth : 0}.max + 1 end On Mon, May 2, 2011 at 11:30 AM, Robert Klemme wrote: > On 02.05.2011 20:17, Mark Hayes wrote: > >> 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 >> > > I think it's pretty OK that way. You could get rid of a bit of redundancy > but whether that actually makes the program better? Judge for yourself: > > def depth > [lchild, rchild].map {|ch| ch ? ch.depth : 0}.max + 1 > end > > def depth > d = 0 > > [lchild, rchild].each do |ch| > d = ch.depth if ch && ch.depth > d > end > > d + 1 > end > > def depth > d = 0 > > [lchild, rchild].each do |ch| > d = [d, ch.depth].max if ch > end > > d + 1 > end > > Kind regards > > robert > > -- > remember.guy do |as, often| as.you_can - without end > http://blog.rubybestpractices.com/ > > -- Mark Hayes mark@deployfx.com --bcaec53961c63d964704a24f5e7f--