From: Robert Klemme Date: 2009-10-26T23:08:42+09:00 Subject: Re: Array Number of Leaves 2009/10/26 Bertram Scharpf : > Hi, > > Am Montag, 26. Okt 2009, 21:17:17 +0900 schrieb Robert Klemme: >> 2009/10/26 salai : >> >    self.inject(0){|acc,x| >> >        if x.kind_of?(Array) >> >          acc += x.number_of_leaves >> >        else >> >          acc += x >> >> The line above should read acc += 1.  You could even simplify that as >> >> acc += (x.number_of_leaves rescue 1) >> >> >        end >> >      } > > I prefer doing it with `case'. I admit I did no performance check. > >  inject( 0) { |acc,x| >    acc += case x >      when Array then   x.number_of_leaves >      else              1 >    end >  } You're right, the solution with "rescue" is rather hackish. :-) My serious solution would be acc += Array === x ? x.number_of_leaves : 1 I prefer to use case for cases (sic!) which have more than two alternatives. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/