Reputation: 22044
how to access the var in the "default" scope in other class definition , do something like this?
var = 1
class MyClass
def self.show
var
end
MyClass.show #=> 1
and BTW I notice the self in the "default" scope return main
, what's this?
Upvotes: 1
Views: 94
Reputation: 42903
It's not possible. Variables defined like var = ...
are always local variables.
Defining a global variable is done by $var = ...
.
Another more hacky approach would be defining an instance variable (@var = ...
), but this would require you to somehow get the main
instance into your MyClass.show
which doesn't seem to be worth the work.
Upvotes: 0
Reputation: 78423
I'm sure there's a better way, but being new to Ruby, I'd use $var
instead of var
. Doing so makes it global. :-)
Upvotes: 3