From: Eustaquio Rangel de Oliveira J Date: 2006-03-26T23:45:10+09:00 Subject: Re: Object methods Hi Robert! > > s.class.class_eval %q(def x.says() puts "x > says:"+self > > end) > This doesn't work for me. You define a method "say" > for an instance "x" > in the scope of the class. You probably want > something else. I noticed that I made a mistake there and correct it on another email few seconds after that one. Sorry about that. My fault. As I told on the other email, the correct thing is s.class.class_eval %q(def s.says() puts "s says:"+self end) So I create a method "says" only on the object s (not on the class String where it belongs to). > It would certainly help if you tell us what problem > you are trying to solve. Ok, not really a problem, I was just curious about it. Let's imagine that I need a way to create methods on *specific objects*, not on their classes. The methods are stored on some file or the user type it when using the program (ok, I know the security concerns about that :-), and I find them later using some reflection and respond_to calls. So at some point the user knows (again, don't know if its a valid situation) that there are some String objects hangin' there, s and x, and says "hey, add this method I defined here on this text field, as a string, to the s object, but *just to it*, not to its class or other String objects". On this case, knowing that I want to create the method just on s, and it's a String, I can use String.class_eval %q(def s.says() puts "s says:"+self end) to make this works, and ask for some reavaliation of the current objects, where it will tell me that the s object, yes, have a "says" method there. s.respond_to?(:says) It works this way, I was just curious if there is not an *object method* to do that, not using the class to make it works. But works this way because of the need to add a virtual class to s to hold the says() method, right? Best regards, ---------------------------- Eust�quio "TaQ" Rangel eustaquiorangel@yahoo.com http://beam.to/taq Usu�rio GNU/Linux no. 224050