Hi --
On Tue, 15 Sep 2009, David Masover wrote:
> On Sunday 13 September 2009 11:18:52 am Gaspard Bucher wrote:
>> 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 ?
>
> As others said, drop the 'define_method'. I'd take it a step further:
>
> module A
> module ClassMethods
> def foo
> puts "'foo' from A"
> end
> end
> def self.included(base)
> base.send :extend, ClassMethods
That's the long way round :-)
base.extend(ClassMethods)
> I would even go so far as to call this a best practice. Thoughts?
The name "ClassMethods" is potentially a bit confusing, since they're
not exactly class methods... but I think it's an effective way to do
what it does. I don't think it's inherently a better practice than,
say, extending a class explicitly in the class -- meaning, I wouldn't
go out of my way to set it up this way if it didn't fall into place
fairly naturally in a give case.
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)