title: Are there good reasons for 'private' to work the way it does in Ruby?
body: <p>It took me a while to understand how <strong>private</strong> methods work in Ruby, and it really strikes me as being very awkward.  Does anyone know if there are good reasons for private methods to be handled the way they are? Is it just historic reasons? Or implementation reasons? Or are there good solid logical reasons (ie. semantic)?</p>
  
  <p>For example:</p>
  
  <pre><code>class Person
    private
    attr_reader :weight
  end
  
  class Spy &lt; Person
   private
    attr_accessor :code
   public
    def test
      code          #(1) OK: you can call a private method in self
      Spy.new.code  #(2) ERROR: cannot call a private method on any other object
      self.code     #(3) ERROR!!! cannot call a private method explicitly on 'self'
      code="xyz"    #(4) Ok, it runs, but it actually creates a local variable!!!
      self.code="z" #(5) OK! This is the only case where explicit 'self' is ok
      weight        #(6) OK! You can call a private method defined in a base class
    end
  end
  </code></pre>
  
  <ul>
  <li>Ruby's behaviour on lines (1), (2) and (5) seems reasonable.</li>
  <li>The fact that (6) is ok is a bit strange, especially coming from Java and C++. Any good reason for this?</li>
  <li>I really do not understand why (3) fails ! An explanation, anyone?</li>
  <li>The problem on line (4) looks like an ambiguity in the grammar, which has nothing to do with 'private'.</li>
  </ul>
  
  <p>Any ideas?</p>