From: Olivier Renaud Date: 2007-02-20T20:45:55+09:00 Subject: Re: Help with Class design > 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 ? > > Thank you all for your help ! > > Chris You want to return a new Signal so... just do it ! def diff() result = [] @signal.each_index do |i| result << @signal[i+1] - @signal[i] unless i == (@signal.length-1) end return MySignal.new(result) end Maybe I missed something in your question, or maybe you would like your class MySignal to act more like an Array. If so, there are many solutions (in order of preference) : * Use the Forwardable module to give access to specific methods from Array * Use the Delegate lib to delegate all unknown methods to Array * Make your class a subclass of Array (but delegation is almost always better) * Reopen Array and add extra features (but this should be reserved for some specific needs, not in your case) I hope I answered your question :) -- Olivier Renaud