Access to class variables?

class Foo
  @@default = "default"

  p instance_variables
  p class_variables

  class << self
    p instance_variables
    p class_variables

    # How do I access the @@default variable here?
  end
end
+3
source share
2 answers

In the same way you do it in any other location @@default.

I'm not sure what I should do p ..(Ruby is not my native language), but it works

class Foo
  @@default = "default"

  class << self
    puts "#{@@default}"
  end
end
+2
source

This question is interesting because it essentially asks: "Is there a way for a metaclass to refer to its" real "class?

, "", "" Ruby , class_variables() . , ...

class Foo
  @@default = "default"
  @@me = self

  p instance_variables
  p class_variables

  class << self
    p instance_variables
    p @@me.class_variables
  end
end
+1

Source: https://habr.com/ru/post/1770405/


All Articles