Hi,

In message "Re: [ ruby-Bugs-21101 ] Clash between accessor methods and local variable declaration"
    on Wed,  9 Jul 2008 03:04:49 -0400 (EDT), <noreply / rubyforge.org> writes:

|Here is my test code :
|
|class X
|	def x;
|		@i;
|	end
|	
|	def x=( other );
|		@i = other;
|	end
|	
|	def initialize
|		@i = 3
|	end
|end
|
|class A < X
|	def initialize
|		super
|		p @i , x 
|	end
|end
|
|class B < X
|	def initialize
|		super
|		x = 4
|		p @i , x
|	end
|end
|
|class C < X
|	def initialize( b = false )
|		super()
|		x = 4 if b
|		p @i , x 
|	end	
|end
|
|class D < X
|	def initialize
|		super
|		self.x = 4
|		p @i , x
|	end	
|end
|
|[ A , B , C , D ].each { |c| p c; c.new;}
|
|And here is the output :
|
|A
|3
|3
|B
|3
|4
|C
|3
|nil
|D
|4
|4

This is not a bug.

* no matter if you have defined a attribute setter or not, simple
  "x = 4" creates a local variable "x".
* if you want to call setter explicitly, you need to call
  "self.x = 4".
* a local variable creation is done at the compile time, so if the
  assignment is not executed, the local variable is initialized to
  nil.
* local variables are higher priority in name resolution, so if you
  have a local variable "x", simple reference to "x" results in local
  variable reference.  you have to specify "x()" to invoke getter
  explicitly.

							matz.