1

Trying to find out how private and protected works when used on class methods I came with this code from some other question:

class Bang
    def instance_bang
      self.class.class_bang
    end

    protected
    def self.class_bang
      puts "bang"
    end
end

Calling instance_bang from an instance of Bang works as expected, however I cannot understand what is different in the following code when I take the approach of using class << self.

class Bang
  def instance_bang
    self.class.class_bang
  end

  class << self
    protected 
    def class_bang
      puts "bang"
    end
  end
end

To me, both pieces of code seems to be the same, but the second one fails with NoMethodError claiming that class_bang is protected.

5
  • One thing you might find confusing is self. at the beginning of self.class.class_bang in the instance method instance_bang. It is not needed, because if no receiver is given, self is assumed. If you're unsure, try it with class.class_bang. in IRB or PRY. Commented Jan 12, 2014 at 9:05
  • Just tried with 2.1.0 and got a syntax error gist.github.com/fgarcia/8382526#file-gistfile1-txt-L3-L5 Commented Jan 12, 2014 at 9:15
  • @CarySwoveland You need to write self.class.class_bang as it is. If you do class.class_bang, MRI sees it as a class keyword. So error you will be getting. Commented Jan 12, 2014 at 9:18
  • I stand corrected. I'll have to have another look in the morning, with fresh eyes. Commented Jan 12, 2014 at 9:32
  • That's interesting. @Arup, I do not dispute that self. is needed to tell Ruby you are referring to the method, not the keyword, but why isn't Ruby smart enough to know that, considering that class is chained to class_bang? self. is needed, for example, when followed by a setter method, to distinguish it from a local variable, but I don't understand the need here, though this is probably not the place to dig into this. Commented Jan 12, 2014 at 19:00

1 Answer 1

2

In the second chunk of code, protected is used to specify visibility of methods of Bang class. But 'def self.class_bang' defines a method on the singleton class of Bang, so the protected key will not apply for the method.

In the first chunk of code, you open singleton class of Bang, so protected is used to specify visibility of methods of singleton class of Bang, this means protected will apply for class_bang method. That is why you get the error.

For more information, read this: http://blog.jayfields.com/2006/11/ruby-protected-class-methods.html

2
  • so that's why! But, does it mean the only way to call Bang.class_bang within Bang#instance_bang is by using the send method? Commented Jan 12, 2014 at 11:46
  • 1
    Yes, because you're not calling a protected method inside its class body or inside its subclass body. Bang is an instance of its Singleton Class, not subclass of its Singleton Class.
    – vidang
    Commented Jan 12, 2014 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.