1

I've got the following Ruby code:

class B
  class << self
    protected
    def prot
      puts "victory"
    end
  end
end
class C < B
  def self.met
    C.prot
  end
end

C.met

which tries to proof that protected class methods are inherited in Ruby. The problem is that if I convert met method to an instance method like this:

class B
  class << self
    protected
    def prot
      puts "victory"
    end
  end
end
class C < B
  def met
    C.prot
  end
end

c = C.new
c.met

it won't work. Maybe it has to do with class and instance methods scope?

user1868607
  • 2,558
  • 1
  • 17
  • 38
  • I get the error: `protected method 'protegido' called for C:Class` - which shows the inheritance does work... it's just that the protection also works. – Myst Aug 04 '15 at 02:06
  • @Rodrigo Did you get clarity on it – pramod Aug 04 '15 at 02:08

3 Answers3

1

It won't work, because the instance of C is not kind_of?(B.singleton_class).

In ruby, a protected method can be called within the context of an object which is kind_of? the class which defines the method, with an explicit receiver which is also kind_of? the class which defines the method.

You defined a protected method on the singleton class of B, so that method can only be called within the objects which are kind_of?(B.singleton_class). The class C inherits B, so C's singleton class inherits B's singleton class, so C is kind_of? B.singleton_class. Thus in your first case, it works. But obviously, C.new is not kind_of? B.singleton_class, so it won't work.

Aetherus
  • 8,720
  • 1
  • 22
  • 36
0

In case of Protected methods, we can call them from the scope of any object belonging to the same class. In your code snippet, as per the scope of the class, method lookup chain picks up that method as it is inherited to super class. It means, you are defining a method in its Singleton class means we can call it by using objects of that class. so we can call it by the object of A and B because of B object inherited from A.

In one word you can call a protected method in a public method of the class. Please refer the below urls for better understanding

http://nithinbekal.com/posts/ruby-protected-methods/

accessing protected methods in Ruby

Community
  • 1
  • 1
pramod
  • 2,258
  • 1
  • 17
  • 22
0

I believe it has to do with the difference of declaring the .protegido method as part of the meta-class (or singleton class) rather than as part of the B class itself.

Read about it here: Metaprogramming in Ruby: It's All About the Self

Myst
  • 18,516
  • 2
  • 45
  • 67