From: Rob Biedenharn Date: 2009-02-05T05:06:55+09:00 Subject: Re: Making object methods available externally On Feb 4, 2009, at 2:03 PM, David Stanford wrote: > Rob Biedenharn wrote: >> On Feb 4, 2009, at 1:37 PM, David Stanford wrote: > >> class CheesePhrase >> def initialize(phrase) >> @phrase = phrase >> say_it >> end >> >> def self.say_it(phrase) >> puts phrase >> end >> def say_it >> self.class.say_it(@phrase) >> end >> end > >> -Rob >> >> Rob Biedenharn http://agileconsultingllc.com >> Rob@AgileConsultingLLC.com > > Thanks a lot, Rob. Is this really the standard way of implementing > something like this? It just seems a little redundant, no? > > Thanks! > > -David When you need it, yes. However, it isn't typical that you'd do something like this. Either the method belongs on the instance or it belongs on the class itself. Occasionally, for convenience, you want a class method to be called on an instance and then you'd likely do it this way. Your example is simple (and, being an example, somewhat contrived), but illustrates a point. You can call #say_it with no parameter because the instance variable is there so an instance *knows* what to say, but to call .say_it on the class, you need to pass the parameter. -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com