From: benjohn@... Date: 2007-02-20T21:26:54+09:00 Subject: Re: Help with Class design >> What you're trying to do with inject is make a more "functional" >> program; you're trying to avoid assigning to things; you're closer to >> saying what you want done, rather than how you want it done >> (declarative >> verses imperative style). If you start making assignments, you break >> away from that style. > > I think I understand what you mean, but I am so used to programming in > languages where assignments are common place (eg. Matlab) that I am > struggling to adjust to this new way of thinking. For example I have a > method : > > def diff() > result = [] > @signal.each_index do |i| > result << @signal[i+1] - @signal[i] unless i == (@signal.length - > 1) > end > result > end > > which takes a @signal object and returns an array of the differences > between each successive element of signal. But now I cannot write > > a = MySignal.new([1,2,3]) > a.diff.variance > > because diff has returned an object of class "array" and the variance > method is not defined . How do I write "diff" in someway that it gives > me a new "signal" instead of an array ? Get it to wrap the array in to a signal, and return that. So, at the end of the method you'll do something like: ... end Signal.new(result) end Cheers, Benj