[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Instance and Related Class Methods

Michael Guterl

8/15/2006 6:20:00 PM

I have a class with a lot of instance methods which are related to
class methods from within the same class.

Example:

class A
def initialize(start_date, end_date)
@start_date, @end_date = start_date, end_date
end

def total_hours
A.total_hours(@start_date, @end_date)
end

def self.total_hours(start_date, end_date)
self.sum(:hours, :conditions => between(start_date, end_date))
end

def total_incoming
A.total_incoming(@start_date, @end_date)
end

def self.total_incoming(start_date, end_date)
self.sum(:incoming, :conditions => between(start_date, end_date))
end

def total_outgoing
A.total_outgoing(@start_date, @end_date)
end

def self.total_outgoing(start_date, end_date)
self.sum(:outgoing, :conditions => between(start_date, end_date))
end
end

Everything works as expected. However, I am wondering if there is a
more Rubyish way of accomplishing this so I can avoid the repetition.
Any other suggestions are welcome. Thanks in advance.

Michael Guterl

2 Answers

e

8/15/2006 6:35:00 PM

0

Michael Guterl wrote:
> I have a class with a lot of instance methods which are related to
> class methods from within the same class.
>
> Example:
>
> <snip />
>
> Everything works as expected. However, I am wondering if there is a
> 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.

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).

> Michael Guterl


--
Posted via http://www.ruby-....

e

8/15/2006 7:25:00 PM

0

Michael Guterl wrote:
> On 8/15/06, Eero Saynatkari <eero.saynatkari@kolumbus.fi> 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-....