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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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 themaininstance into yourMyClass.showwhich doesn’t seem to be worth the work.