From: Dave Thomas Date: 2001-10-26T01:38:46+09:00 Subject: [ruby-talk:23316] Re: why the @ s ? Tobias Reif writes: > Dave, > > thanks for your explanation. But I'm still confused: (thanks for your > patience) > > > a = 10 > > def fred > > p a #=> -:3:in `fred': undefined local variable or method `a' > > end > > with ruby 1.6.3 (2001-03-19) [i386-cygwin] : > -------------------- > a = 10 > def fred > p a #=> no error > end you have to call 'fred' to get the error... > -------------------- > > works: > -------------------- > var01 = 'var01' > > def print_var var > print var > end > > print_var var01 #=> prints 'var01' Because you're passing 'var' in as a parameter > doesn't: > -------------------- > var01 = 'var01' > > def print_var01 > > print var01 > > end > > print_var01 > > # 18:in `print_var01': undefined local variable or method `var01' for > # # (NameError) > -------------------- var01 is not defined inside print_var01 > In the last example: how could I make it work without adding an @ > sign? You can't. Local variables are no in scope within the method. > in the last line? Since print_var01 is defined as method just above; why > doesn't Ruby treat it as such? It does - the problem is the 'var01' variable. Regards Dave