I saw this in some code today.
class Foo
def self.bar
@myvar = 'x'
end
end
What exactly is this accessing? As far as I can tell, this isn’t accessible form instance methods. What’s this called (something google-able) as I cant seem to find examples of this anywhere else.
The
@myvarsyntax identifiesmyvaras an instance variable so the real question is this:And the answer is “
selfis the class object”. So,@myvaris an instance variable of theFooclass object. If you add another class method:And then do this:
You’ll see
xon the standard output.