From: walter@... Date: 2004-07-03T05:38:17+09:00 Subject: Re: define_method to add a Class method? > > I guess I just want to know if it can be done using a define_ method > > or do we _have_ to use eval to add a class method this way. > > Well, I can add a method to that same class I created with an eval, > this way: > > class MyClass > define_method(:my_method2) do > print("Hello, world, again!\n") > end > end > > o.my_method2 #=> "Hello, world, again!\n" > > > Sean O'Dell > that does work, but you are adding a method to an instance of the class. I want to add a method to the class itself, and I want to be able to do it to an anonymous class. At this point this is purely academic. I have a working version, I just think there is something I am not understanding as to why I can seem to be able to dynamically add a class method. I can add dynamic instance methods fine. I am looking for the equivalent of theClass = Class.new do def self.class_method puts "class_method called" end end theClass.class_method but using define dynamically (it would really be returning something that was passed into the method that is creating the class) def create(data) Class.new{define_method('self.class_method', lamda{data.dup})} end puts create(%w{we made it}).class_method.join(" ") and get "we made it" that's the gist of what I am trying to do. As I said, its purely academic at this point, just a curiosity. Thanks, Walt ***************************************************** Walter Szewelanczyk IS Director M.W. Sewall & CO. email : walter@mwsewall.com 259 Front St. Phone : (207) 442-7994 x 128 Bath, ME 04530 Fax : (207) 443-6284 *****************************************************