From: Tony Arcieri Date: 2010-10-07T07:37:58+09:00 Subject: Re: Missing @ for instace variable in class, but no error message --001636458e8ab947fd0491fa6d34 Content-Type: text/plain; charset=ISO-8859-1 attr_accessor defines the Test#a method which is a getter for the @a instance variable On Wed, Oct 6, 2010 at 4:11 PM, Stefan Salewski wrote: > Some days ago I was defining a class, but forgot to use the leading @ > for an instance variable. But it still works -- that variable was made > accessible by attr_accessor statement. So my question: > > Why gives the statement "puts a" no error message? In my opinion it > should give one, I forgot to use @. The "attr_accessor :a" was only > there to allow access from outside of that class, for instance > variables. > > #!/usr/bin/ruby -w > class Test > attr_accessor :a > def initialize > @a = 0 > @b = 1 > end > def show_values > puts a # should be @a, but seems to work > puts b # gives an error, as expected > end > end > t = Test.new > t.show_values > > Output is: > ruby --version > ruby 1.8.7 (2010-08-16 patchlevel 302) [x86_64-linux] > > ruby test.rb > 0 > test.rb:11:in `show_values': undefined local variable or method `b' for > # (NameError) > from test.rb:16 > > Best regards, > > Stefan Salewski > > > > -- Tony Arcieri Medioh! A Kudelski Brand --001636458e8ab947fd0491fa6d34--