From: Robert Dober Date: 2007-02-20T20:51:59+09:00 Subject: Re: Help with Class design On 2/20/07, Chris Lowis wrote: > > 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 ? > def diff r= [] @signal.inject{|a,b| r << (b-a); b} Signal.new( r ) end HTH Robert > Thank you all for your help ! > > Chris > > > > -- > Posted via http://www.ruby-forum.com/. > > -- We have not succeeded in answering all of our questions. In fact, in some ways, we are more confused than ever. But we feel we are confused on a higher level and about more important things. -Anonymous