title: Understanding Ruby class instance variables
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/44715/why-do-ruby-setters-need-self-qualification-within-the-class">Why do Ruby setters need &ldquo;self.&rdquo; qualification within the class?</a>  </p>
  </blockquote>
  
  
  
  <p>Can someone explain the difference between the following, and why it isn't as one might expect:</p>
  
  <pre><code># version #1
  class User
    def initialize(name, age)
      @name = name
      @age = age
    end
  end
  
  #version #2
  class User
    attr_accessor :name, :age
    def initialize(name, age)
      @name = name
      @age = age
    end
  end
  
  #version #3
  class User
    attr_accessor :name, :age
    def initialize(name, age)
      self.name = name
      self.age = age
    end
  end
  </code></pre>
  
  <p>From what I understood, in methods, when you are assigning, you have to use the <code>self</code> keyword. Why can't you use this in an <code>initialize</code> method?  Or can you? I tried using it and it didn't seem to work as expected, I'm just confused as to which technique to use and when and more importantly <em>why</em>.</p>
  
  <p><strong>I really hope someone can clear this up for me once and for all :)</strong></p>