Consider the following sample ruby class
class User
def hello
puts "hello"
end
end
now, for initialization. there are two ways
normal variable
1.9.3p125 > tr = User.new
=> #<User:0x98778c4>
1.9.3p125 > tr.hello
Hello world
=> nil`
Instance variables:
1.9.3p125 > @tr = User.new
=> #<User:0x9890f2c>
1.9.3p125 > @tr.hello
Hello world
=> nil
Now, in both cases it works the same. so what is the difference between normal variable vs instance variable?
A normal variable has scope only within the current context; an instance variable has scope throughout one instance of a class. In your case they’re confused because the context is
main, which acts as an instance ofObject.Consider the following, which may make things clearer