I understand that local variables are limited to the scope they were declared in and instance variables exist as long as the class exists, but what happens if you declare a local variable in the class scope without prefixing it with @? Doesn’t that implicitly it is an instance variable, even though you didn’t use an @ to declare it as one?
I understand that local variables are limited to the scope they were declared in
Share
They exist as long as the object exist. Instance variables are per-object, not per-class.
Then the variable is in scope within the class definition, but not within any
defs inside that class definition as those introduce a new scope.No.
If you use
define_methodinstead ofdefto create methods, the local variable will be accessible within the methods, but since the variable only exists once (not once per object), they’d act more like class variables than instance variables in that case. I also can’t think of a good reason why you’d use them that way.