logo       

Re: ActiveRecord: private versus protected methods: msg#00230

lang.ruby.rails.core

Subject: Re: ActiveRecord: private versus protected methods

On 9/27/06, urbanus <urbanus@xxxxxxxxx> wrote:
I'm considering subclassing ActiveRecord::Migrator to enhance the way
it tracks the migration version number of a database.  However there's
a hurdle: it has private methods.

Private methods are a major stumbling block for anyone needing to
sub-class something.  If the public methods of the class call the
private methods, you can't sub-class without replacing all the private
methods (because otherwise they won't be available in the descendant
class).

irb(main):005:0> class C; def bar() foo end; def foo() puts 'foo' end; private :foo end
=> C
irb(main):006:0> class D < C; end
=> nil
irb(main):007:0> D.new.foo
NoMethodError: private method `foo' called for #<D:0x2aaaab4eab10>
        from (irb):7
irb(main):008:0> D.new.bar
foo
=> nil


A much better approach is to declare methods as 'protected'.  That
offers a lot more flexibility:

* The methods are still protected from being called from outside the
class.

* The class can be sub-classed, and it will still work.

Does anyone know: is there a compelling reason for using 'private' in
this case?  Are there any core developers listening?  Can you make your
methods 'protected' instead of 'private' please?

You need protected only if you wish the call the method from a subclass. The inherited public method is not affected. Please bring up specific examples (preferably as Trac tickets) and we will address them.

Best,
jeremy

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To post to this group, send email to rubyonrails-core@xxxxxxxxxxxxxxxx
To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@xxxxxxxxxxxxxxxx
For more options, visit this group at http://groups.google.com/group/rubyonrails-core
-~----------~----~----~----~------~----~------~--~---

<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise