Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to access class attribute, which was set by class_exec()

This is what I’m trying to do:

class Foo; end

Foo.class_exec do
  class_variable_set(:@@x, 42)
  def get
    @@x # runtime error here!
  end
end

puts Foo.new.get

I’m getting (Ruby 3.2.1):

$ ruby a.rb
a.rb:6:in `get': class variable access from toplevel (RuntimeError)
    from a.rb:10:in `<main>'

How can I read this @@x, which I just set?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There’s class_variable_get, but since it’s a method of Module, have to call it on the receiver’s class:

Foo.class_exec do
  class_variable_set(:@@x, 42)
  def get
    self.class.class_variable_get(:@@x)
  end
end

Alternatively, it’s perfectly fine in Ruby to re-open a class:

class Foo; end

class Foo
  @@x = 42

  def get
    @@x
  end
end

Note that it’s usually discouraged to use class variables, see Why is using a class variable in Ruby considered a ‘code smell’?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading