From: Eero Saynatkari Date: 2006-08-16T04:25:02+09:00 Subject: Re: Instance and Related Class Methods Michael Guterl wrote: > On 8/15/06, Eero Saynatkari wrote: >> > more Rubyish way of accomplishing this so I can avoid the repetition. >> > Any other suggestions are welcome. Thanks in advance. >> >> The simplest thing would be to eliminate these calls on >> the instances completely since no instance data is needed. > > > I'm not sure I understand. I would like to have access to both class > and > instance methods. Basically some of my code looks like this. Yes, I apologise, I misread this as a simpler scenario. In your case, there is no simple automation unless you can generalise how the mapping from instance- to class methods would work (for example, if all the methods pass @start_date and @end_date you could map it easily). Alternately you could modify the class methods to accept an instance and extract the data from it--while of course still accepting the times when no instance is available. This way you can automate a missing instance method mapping to call the class method with the instance as the argument. The #method_missing, er, method works for this. There is no built-in way to achieve this, though. > day = A.new(1.days.ago, Time.now) > hour = A.new(1.hour.ago, Time.now) > > Then I would also like to be able to use arbitrary starting and ending > ranges with the class method, without the need to create an instance. > > total_incoming = A.total_incoming(1.year.ago, Time.now) > > If you do want to do this, you can automate it by creating >> a #method_missing handler for the instance methods--it will >> check whether a class method by the same name exists and >> then forward to it (falling back on the normal #method_missing >> on failure). -- Posted via http://www.ruby-forum.com/.