# If i don't use @n but use n instead, there is no error

**URL:** <https://www.ruby-forum.com/t/if-i-dont-use-n-but-use-n-instead-there-is-no-error/114619>\
**Category:** Ruby\
**Created:** [September 17, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/if-i-dont-use-n-but-use-n-instead-there-is-no-error/114619 "2007-09-17T13:23:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 17, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/if-i-dont-use-n-but-use-n-instead-there-is-no-error/114619/1 "2007-09-17T13:23:05Z")

</div>

in an instance method, if i didn’t use @n but used n, the program still  
runs…  
and it is not changing the global n… so what is it changing?

Example:

n = 10

class Foo

def initialize(i)  
@n = i  
end

def change()  
puts  
puts “Inside of change!!!”  
n = 111111 # intentionally not using @n  
end

def print\_it  
puts  
p “Printing Object”  
p @n  
end

end

foo = Foo.new(3)  
foo.print\_it

foo.change  
foo.print\_it

## puts p “Global var” p n -------- output

C:\rails\depot\>ruby test\_class01.rb

“Printing Object”  
3

Inside of change!!!

“Printing Object”  
3

“Global var”  
10

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 17, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/if-i-dont-use-n-but-use-n-instead-there-is-no-error/114619/2 "2007-09-17T13:25:24Z")

</div>

kendear wrote:

> def initialize(i)  
> puts

well if oyou would like global n you should use $n, for me this kode  
looks good and proper, change method changes local variable n, not  
global one

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 17, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/if-i-dont-use-n-but-use-n-instead-there-is-no-error/114619/3 "2007-09-17T13:23:39Z")

</div>

kendear wrote:

> in an instance method, if i didn’t use @n but used n, the program still  
> runs…  
> and it is not changing the global n… so what is it changing?

The variable ‘n’ local to the current context, which is the method  
‘change’ =\> the variable disappear as soon as the code exists change().

Lionel
