On Sun, 26 Mar 2006, Eustaquio Rangel de Oliveira J wrote:

> Hello, Robert!
>
>> I'm not sure what exactly you're after.  Do you mean
>> class String
>>    def says
>>      print "s says: ", self, "\n"
>>    end
>> end
>> ?
>
> Not really, I want to create a new method on the
> object, after it's created, but not on the class. As
> the previous example of
>
> s = "Testing"
> class <<s
>   def says
>      print "s says: ", self, "\n"
>   end
> end
>
> where it creates the method (and all that metaclass
> stuff).
>
> At this point just, just s have the "says" method, not
> the class String or any other String object that was
> created or will be created. Using
>
> s.class.class_eval %q(def x.says() puts "x says:"+self
> end)
>
> have the same effect, but what I like to know if there
> is another way to do that. I mean, create a method on
> a specific object (on this case, just "s"), not on the
> class.

How about:

   def s.says
     puts "s says: #{self}"
   end

?


David

-- 
David A. Black (dblack / wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black