On Sep 24, 7:40 pm, Tom M <thomas.mack... / gmail.com> wrote:
> > This isn't a solution for your problem, but a comment on the
> > (possible?) root of it: IMO, method_missing shouldn't fire on private/
> > protected method calls. My opinion goes further, that calling a
> > private or protected method shouldn't raise the same exception as
> > calling a method that doesn't exist. If for some reason one wants to
> > track attempted access to private methods, rescuing NoMethodError and
> > checking text is somewhat unsavory. It would be nicer to rescue
> > MethodAccessLevelError (or something with an even better name), which
> > could be related to NoMethodError based on use cases (right now, I see
> > them as children of the same parent).
>
> Your suggestion seems to violate the notion of encapsulation.  If an
> change were to be made, I'd think it would be better to make the error
> the same for calling a parent's private method as it is for calling a
> non-existant method.

A parent's private method? I'm not sure I follow. Do you mean
something like the following?


class A
  private

  def priv_method
    'private'
  end
end

class B < A
end

b = B.new
b.priv_method


Because that's not calling a parent private method. The private method
is inherited from A and part of B now.

My suggestion might violate encapsulation. It's not necessary to
change which exceptions are raised, but I am convinced that
method_missing shouldn't come into the picture when attempting to call
a private or protected method. I took a look at the method_missing
code and it specifically checks for different reasons why the desired
method couldn't be called, including access level. And it uses the
NoMethodError exception.

It seems to me that there are two viewpoints to consider. From outside
the class, there's no difference between calling a private or
protected method and a method that doesn't exist: it simply doesn't
work. But a private method actually exists, so it is not exactly the
same as an undefined method. With that in mind, I go with my feeling
that they should be treated differently.

--
-yossef