I'm trying to write a method that prints class variable names and their values. As an example:
class A
def printvars
???
end
end
class <<A
def varlist(*args)
???
end
end
class B < A
varlist :c
def initialize(c)
@c = c
end
b = B.new(10)
b.printvars()
And I would like the output to be c => 10
. But I don't know what goes in the ???
. I've tried using a self.class_eval
in the body of varlist, but that won't let me store args
. I've also tried keeping a hash in the class A
and just printing it out in printvars
, but the singleton class is a superclass of A
and so has no access to this hash. So far everything I've tried doesn't work.
I think something similar must be possible, since Rails does something related with its validates_* methods. Ideally I could make this work exactly as expected, but even a pointer to how to print just the variable names (so just c
as output) would be most appreciated.