From: walter@... Date: 2004-07-03T21:32:56+09:00 Subject: Re: define_method to add a Class method? > Hi, > > At Sat, 3 Jul 2004 08:46:48 +0900, > walter@mwsewall.com wrote in [ruby-talk:105154]: > > def create(data) > > Class.new do > > my_lambda = lambda{data} > > define_method("instance", my_lambda) #this is fine > > > (class << self; self; end).class_eval do > > define_method("ok_method"){"ok method"} #this is fine > > define_method("data_passed_in", my_lambda) > > end > > end > > end > > Or: > > class Result > def instance > self.class.data_passed_in > end > class << self > def ok_method > "ok_method" > end > def data_passed_in > @data > end > end > end > > def create(data) > Class.new(Result) {@data = data} > end > > -- > Nobu Nakada > Thanks Nobu, The part I missed was setting @data within the scope of the new class. so this now works.. def create(data) Class.new do @data = data class << self define_method("data_passed_in"){@data} end define_method("q"){"we are in q"} end end create("some data").data_passed_in Now that I look at it this morning I can't believe I missed that. Also I didn't realize that you could pass in a Class while creating an anonymous class and extend that in the body of new. Very cool. Thanks again, I knew I was missing something. 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 *****************************************************