Well, like the title shown, what’s the difference between the two class instance variables below
class Document
@default_font = :Arial
...
end
And
class Document
def foo
@default_font = :Arial
end
...
end
Is there anyone can explain it to me. Thank you very much.
In your first case, the variable is neither a class variable(which should have started with
@@, nor an instance variable. It is simply a local variable not available outside the current scope, not even within the instance methods.The second is an instance variable.
The
@var1 + 6inside the instance method gives an error.