7stud -- wrote:
>> Well, then what's all this talk about "the parser sees this, and the 
>> parser does that".  

If you do this:

class B
  def a
    @a
  end

  def bar
    "bar"
  end

  def baz
    self.a = "baz"
  end
end

b = B.new
puts b.bar
puts b.a
puts b.baz

--output:--
bar
nil
'baz': undefined method `a=' for #<B:0x24fe0> (NoMethodError)

1) b.a returns nil because the 'a' method exists, but the 'a' method 
tries to return a non-existent instance variable @a.

2) The error message is due to the fact that self.a tries to access a 
method called a=, which in this class doesn't exist.

If you add the setter method, a=, then the error message goes away:

class B
  def a
    @a
  end

  def a=(val)
    @a = val
  end

  def bar
    "bar"
  end

  def baz
    self.a = "baz"
  end
end

b = B.new
puts b.bar
puts b.a
puts b.baz

--output:--
bar
nil
baz
baz

Note that 'a =' doesn't appear anywhere in the code.  Adding 'a =' in 
the body of a method doesn't change anything:

class B
  def a
    @a
  end

  def a=(val)
    @a = val
  end

  def bar
    a = "bar"  #<----****
  end

  def baz
    self.a = "baz"
  end

  def foo
    a = 10
  end
end

b = B.new
puts b.bar
puts b.a
puts b.baz
puts b.a


--output:--
bar
nil
baz
baz


All of which I guess has nothing to do with the op's question.  The op 
correctly identified that self.a = "baz" calls the a= method, but that a 
= "bar" does not.   I guess I would pose this question to the op: when 
have you ever been able to access the a= method in a class without using 
the format some_obj.a = ?



-- 
Posted via http://www.ruby-forum.com/.