Hi --

On Mon, 14 Sep 2009, Gaspard Bucher wrote:

> David A. Black wrote:
>> Hi --
>>
>> On Mon, 14 Sep 2009, Gaspard Bucher wrote:
>>
>>> Example ruby code: http://bit.ly/2NjP5w
>> It's a matter of the order of method lookup. In general, an object
>> looks for a method first in its class, and then in modules mixed into
>> that class.
>>
>
> Damned ! This means that all the dynamic methods created by rails with
> "has_one" and such cannot be overwritten by including a module except if
> you write:
>
> module A
>  def self.included(base)
>    base.send(:define_method, 'foo') do
>      puts "'foo' from A"
>    end
>  end
> end
>
> Any other solution ?

I would consider rewriting that as:

   module A
     def self.included(base)
       base.class_eval do
         def foo
           puts "'foo' from A"
         end
       end
     end
   end

so as to normalize it back to the "def" form.

Keep in mind, too, that this is a bit fragile because the order
matters. If you include the module first and then do has_many, (or
attr_accessor, or any other instance-method generator), the has_many
will "win".


David

-- 
David A. Black, Director
Ruby Power and Light, LLC (http://www.rubypal.com)
Ruby/Rails training, consulting, mentoring, code review
Book: The Well-Grounded Rubyist (http://www.manning.com/black2)