Kenneth
(Kenneth)
1
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
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?
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
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
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