JoMojo
JoMojo

Reputation: 404

Ruby class << self variable, accessing from another class

I'm having a trouble figuring out how to access a variable in a class << self from another class. I've searched and found many questions close to this but not exactly:

class << self
  @@var="foo"
end

class A
  puts @@var
end

I've even tried creating class A within class << self with no luck either. How can I access @@var?

Upvotes: 1

Views: 231

Answers (1)

David Grayson
David Grayson

Reputation: 87386

You can do this:

class << self
  @@var="foo"
end

self.class.class_variable_get :@@var

But I'm really skeptical that you actually need to use a class variable in a singleton class. It seems complicated. Maybe you could ask another question explaining what you're really trying to do.

Upvotes: 2

Related Questions