How do I set data at the eigenclass level (data should basically be
connected to the class name)?
The @@my_data syntax seems to attach only to the original class in which
it was defined, not for any subclasses. And the @my_data syntax attach
to each instance.
I think I should be able to solve it “manually” with a hash, but I’m
looking for a built-in syntax for it?
The @@my_data syntax seems to attach only to the original class in which
it was defined, not for any subclasses. And the @my_data syntax attach
to each instance.
I am not sure if I understand you. Class variables(@@bar) are
available for its children:
(in irb)
The @@my_data syntax seems to attach only to the original class in which
it was defined, not for any subclasses.
You can use attributes (@var) at the class level and manually copy
values with the Class#inherited method.
class A
class << self
attr_accessor :foo
def inherited(sub)
sub.foo = @foo
end
end
self.foo = 1
end
class B < A
end
p A.foo, B.foo
# => [1, 1]
B.foo = 2
p A.foo, B.foo
# => [1, 2]
If something like this is what you were asking for.
Or you could use some package that does this for you. IIRC traits
implements such a thing.
Did you try? Actually eigenclass is not up to date. Matz changed and
later rolled back many things. It works for me, so I guess it’s one of
these things.
Did you try? Actually eigenclass is not up to date. Matz changed and
later rolled back many things. It works for me, so I guess it’s one of
these things.