On 06.05.2007 17:37, Nanyang Zhan wrote: >> OP, what do you want - Robert's solution or Robert's solution? :-) >> >> Kind regards >> >> robert > Thanks RobertS! > Now I'm pretty sure that people tend to using the programming languages > that has the same character with their names, like Roberts will pick up > Ruby while Johns prefer Java. Actually I do Java for a living - and I don't hate it. :-) > Robert Dober's solution offer me details on how to write the codes about > defining those methods. It will be very useful, but still I have no idea > where to put these definitions. > Robert's solution(yes, that's Robert Klemme) needs to be adjusted to fit > my requirement correctlly. But it's very interesting. Robert, Can you > tell me how to make setup_methods :apple can be called inside a class, > like: > > #magic codes put here, then > Class Fruit < ActiveRecord::Base > # create all the methods on the fly > setup_methods :apple > setup_methods :banana > ... > # call methods that are created on the fly inside a class > after_save :save_apple > after_save :save_banana > end > > That is, please tell me how to mixin. Basically you can define a method setup_methods in class Class. Untested: class Class def setup_methods(sym) class_eval do attr_accessor sym end class_eval "def #{sym}_save() puts 'saving #{sym}' end" end end Kind regards robert