From: Rob Biedenharn Date: 2009-02-05T03:52:38+09:00 Subject: Re: Making object methods available externally On Feb 4, 2009, at 1:37 PM, David Stanford wrote: > Hi all, > > I'm having trouble figuring out a way of creating a method within a > class that's available not only to the other object methods (object > instances), but also externally, as if I were to use 'self.method'. > Below is an (admittedly, lame) example of what I'm referring to: > > ############################# > > class CheesePhrase > def initialize(phrase) > @phrase = phrase > say_it > end > > def say_it(phrase=@phrase) > puts "#{phrase}" > end > end > > ############################# > > Thanks in advance! > > -David 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 irb> some_cheese_phrase = CheesePhrase.new("I like cheese.") I like cheese. => # irb> some_cheese_phrase.say_it I like cheese. => nil irb> CheesePhrase.say_it("I like cheese, too.") I like cheese, too. => nil -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com