From: "johanatan " Date: 2005-12-05T10:44:27+09:00 Subject: Re: injecting dynamic methods into a class transfire wrote: > If you don't mind while I'm at this I'm going to touch up the code to > follow ruby conventions. I don't mind at all. I'm new to ruby so I haven't picked up on the extensions yet. :) > > def initialize( attr_and_sizes_hash, consumer_class ) > attr_and_sizes_hash.each {|attr_name, max_size| > code = %{ > def #{attr_name}=( file ) > write_attribute( '#{attr_name}', ImageBlob.getBlob( file, > #{max_size} ) ) > end > } > consumer_class.class_eval code > end > I didn't try class_eval. Did you test this or is it just a theory? :) Seemed like regular eval and extend should work. > BTW it seems odd to use the #initialize method of ImageBob to do this. It is odd, but the ImageBlob class contains only one class method (i.e., static method) and no data members, so no actual memory should be defined for it. I suppose it could have contained two class methods instead of using initialize (that is better so I'll change that). At first, I was trying to store a ptr to the ImageBlob instance in the consumer class but realized it wasn't necessary. Much thanks for the answers. -- Posted via http://www.ruby-forum.com/.