From: dblack@... Date: 2007-02-20T21:08:35+09:00 Subject: Re: Help with Class design Hi -- On Tue, 20 Feb 2007, Chris Lowis wrote: > Thank you all for your help, knowing exactly what "self" refers to in > each context seems to be the stumbling block for me at the moment. I'll > have to read about this some more, along with the documentation for some > of the Enumerables . > >> I suppose that you variance method should read something like >> >> def variance >> m = mean >> @signal.inject(0) { |sum, x| sum + (x-m)**2 } / @signal.length >> end >> >> Note that I prefer not using meaningless assignment in the inject block. > > And to that end, using the suggestions above, I suppose I could equally > well write : > > def variance() > @signal.inject(0) { |var, x| var += (x - mean) ** 2 } / > (@signal.length-1) > end > > removing the assignment "m = mean" ? The disadvantage of that is that it will call the method mean once for each iteration. So you're better off storing it in a local variable. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)