title: Understanding private methods in Ruby
body: <pre><code>class Example
   private
   def example_test
    puts 'Hello'
   end
  end
  
  e = Example.new
  e.example_test
  </code></pre>
  
  <p>This of course will not work, because we specified explicit receiver - instance of Example (<code>e</code>), and that is against a "private rule".</p>
  
  <p>But I cannot understand, why one cannot do in Ruby this:</p>
  
  <pre><code>class Foo
   def public_m
    self.private_m # &lt;=
   end
   private
   def private_m
    puts 'Hello'
   end
  end
  
  Foo.new.public_m
  </code></pre>
  
  <p>The current object inside <code>public_m</code> method definition (i.e. <code>self</code>) is the instance of Foo. So why it is not allowed? To fix that I have to change <code>self.private_m</code> to just <code>private_m</code>. But why this differ, isn't the <code>self</code> an instance of Foo inside <code>public_m</code>? And who is the receiver of bare-word <code>private_m</code> call? Isn't that <code>self</code> - what actually you omit because, Ruby will do it for you (will call private_m on self)?</p>
  
  <p>I hope I didn't confuse it too much, I am still fresh to Ruby. </p>
  
  <hr>
  
  <p>EDIT:
  Thank you for all the answers. Putting them all together I was able (finally) to grok the obvious (and not so obvious for someone, who have never seen things like Ruby): that <code>self</code> itself can be
  explicit and implicit receiver and that make the difference. So there are two rules, if you want to call a private method: <code>self</code> must be implicit receiver, and that self must be an instance of current class (<code>Example</code> in that case - and that takes place only when self if inside instance method definition, during this method execution). Please correct me if I am wrong.</p>
  
  <pre><code>class Example 
  
   # self as an explicit receiver (will throw an error)
   def explicit 
    self.some_private_method
   end
  
   # self as an implicit receiver (will be ok)
   def implicit
    some_private_method
   end
  
   private
  
   def some_private_method; end
  end
  
  Example.new.implicit
  </code></pre>
  
  <p>Message for anyone who could find this question during the google trails: this may be helpful - <a href="http://weblog.jamisbuck.org/2007/2/23/method-visibility-in-ruby" rel="noreferrer">http://weblog.jamisbuck.org/2007/2/23/method-visibility-in-ruby</a></p>