Project

General

Profile

Actions

Bug #12216

closed

Class inherited Singleton method exclude argument from protected method accessing

Added by adamluzsi (Adam Luzsi) over 6 years ago. Updated over 6 years ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
1.8.7-p375 && 1.9 && 2.+
[ruby-core:74542]

Description

Dear Ruby Lang developers.

I would like to ask about why the following code can work?

Or is it a valid scenario and "as expected" behavior to ignore public call on a protected method.

Thank you guys for the brilliant!

class A
  class << self
    def inherited(klass)
      p klass.protected_method_call
    end

    protected
    def protected_method_call
      self.to_s
    end
  end
end

class B < A
end

# output: "B"

Updated by nobu (Nobuyoshi Nakada) over 6 years ago

  • Description updated (diff)
  • Status changed from Open to Rejected

Expected behavior.
In the inherited method, self and klass are A and B respectively, which are instances of Class.
Protected methods can be called from a same class instance.

Updated by adamluzsi (Adam Luzsi) over 6 years ago

Nobuyoshi Nakada wrote:

Expected behavior.
In the inherited method, self and klass are A and B respectively, which are instances of Class.
Protected methods can be called from a same class instance.

Thank you very much!
My mistake, I mixed up with the private scope.
I'm terrible sorry for this.

Best regards,
Adam L.

Updated by adamluzsi (Adam Luzsi) over 6 years ago

Nobuyoshi Nakada wrote:

Protected methods can be called from a same class instance.

So far I though, A and B is a different class instance, just they are in inheritance relationship.

But I see now,
class instance != object instance
class instance is based on inheritance

Actions

Also available in: Atom PDF