From: ts Date: 2002-09-23T21:17:44+09:00 Subject: Re: alias/method redfinition >>>>> "a" == ara howard writes: a> ts wrote in message news:<200209191714.g8JHEeb09103@moulon.inra.fr>... >> >>>>> "a" == ara howard writes: >> >> You use #eval which is evil a> if the eval'd string is constant, why? There are 2 problems in your implementation 1) you use #module_eval to compile and execute a String which never change, and this each time that #fields is called. 2) to use #module_eval you create a new anonymous module and because you don't want to have multiple include for the same object you need to protect the call with @oldcalls These 2 problems can be easily solved with a module 1) the module is compiled only once (when ruby compile the script) and not for each call of #fields 2) ruby never try to include twice the same module, This mean that in #fields you can just write def fields=(*fields) extend M # etc end If the module was previously included ruby do nothing otherwise it execute the include If I've written `extend M unless @fields' this is just to make a little faster (i.e. don't try to call #extend if ruby do nothing) You have one case where #module_eval create more problems than it solve a> this works. though i do not understand how the super remains bound to a> the old [] after the call 'extend M' The new methods #[] and #[]= are created in the module M and they don't erase the old definitions which are in the class Hash When you write extend M ruby will create a proxy class which is inserted just before the class Hash, this will give a class (and methods) specific to this object When you call #[] (#[]=) for this object, ruby find these methods in the proxy class, and with a call to #super ruby will retrieve the methods stored in Hash You have something like this * before extend, the inheritance for the object is Hash ==> [Enumerable] ==> Object ==> [Kernel] * after extend for this same object (singleton class) ==> [M] ==> Hash ==> [Enumerable] ==> Object ==> [Kernel] [M] is a proxy class to M [Enumerable] is a proxy class to Enumerable -- Guy Decoux