Data for eigenclass

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?

Best regards,

Jari W.

On Wed, Feb 6, 2008 at 10:05 AM, Jari W.
[email protected] wrote:

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)

class Foo
@@foo = “Yeehaaw!”
end

class Bar < Foo
def initialize
puts @@foo
end
end

Bar.new
=> “Yeehaaw!”

available for its children:
No longer in Ruby 1.9
http://eigenclass.org/hiki/Changes+in+Ruby+1.9#l38

Cheers,
Alex

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.

Thomas.

I am not sure if I understand you. Class variables(@@bar) are
available for its children:
No longer in Ruby 1.9
http://eigenclass.org/hiki/Changes+in+Ruby+1.9#l38

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.

$ ruby1.9 -v
ruby 1.9.0 (2008-01-14 revision 0) [i686-linux]
$ ruby1.9 -e “class A; @@a = 1; end; class B < A; @@a end”


Rados³aw Bu³at

http://radarek.jogger.pl - mój blog

tho_mica_l wrote:

            sub.foo = @foo
B.foo = 2
p A.foo, B.foo
# => [1, 2]

If something like this is what you were asking for.

Thanks! This was what I was looking for!

Or you could use some package that does this for you. IIRC traits
implements such a thing.

I’ll look at it as well!

Best regards,

Jari W.

2008/2/6 Rados³aw Bu³at [email protected]:

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.

$ ruby1.9 -v
ruby 1.9.0 (2008-01-14 revision 0) [i686-linux]
$ ruby1.9 -e “class A; @@a = 1; end; class B < A; @@a end”

It works in 1.8.6 for me:

C:>ruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]

C:>ruby -e “class A; @@a=1; puts @@a; end; class B < A; puts @@a end”
1
1

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs