First of all: This is a bad idea in general since you shouldn’t use class variables in ruby.
But just in case you ever need to use them and would like to access them using a getter method, this is how it can work:

attr_class.rb
1
2
3
4
5
6
7
8
9
class Derp
  @@numero_uno = "Something about cake"
  @@numero_dos = "Number one was a lie"
  #this creates the methods when the class is loaded  
   self.class_variables.each{ |sym| class_eval("def self.#{sym.gsub('@@','')}; return #{sym}; end;") }
end

puts Derp.numero_uno
puts Derp.numero_dos

This is a bit of magic and even has an eval(), but it works…

Comments