From: "Jonathan Date: 2005-12-06T10:56:33+09:00 Subject: Re: injecting dynamic methods into a class Here's the updated code: require 'RMagick' include Magick module ImageBlob def ImageBlob.inject_assigners( 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.get_blob( file, #{max_size} )) end } consumer_class.class_eval code } end def ImageBlob.get_blob( file, max_image_size ) begin img = Image.from_blob( file.read ).first return nil if not img img.format = "JPG" if img.rows > max_image_size or img.columns > max_image_size if img.rows > img.columns factor = max_image_size / img.rows.to_f else factor = max_image_size / img.columns.to_f end img = img.scale( factor ) end ret_val = img.to_blob GC.start return ret_val rescue return nil end end end -- Posted via http://www.ruby-forum.com/.