# Data for eigenclass

**URL:** <https://www.ruby-forum.com/t/data-for-eigenclass/128267>\
**Category:** Ruby\
**Created:** [February 6, 2008, 10:06am UTC](https://www.ruby-forum.com/t/data-for-eigenclass/128267 "2008-02-06T10:06:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [February 6, 2008, 10:06am UTC](https://www.ruby-forum.com/t/data-for-eigenclass/128267/1 "2008-02-06T10:06:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [February 6, 2008, 10:20am UTC](https://www.ruby-forum.com/t/data-for-eigenclass/128267/2 "2008-02-06T10:20:30Z")

</div>

On Wed, Feb 6, 2008 at 10:05 AM, Jari W.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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!”

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [February 6, 2008, 12:28pm UTC](https://www.ruby-forum.com/t/data-for-eigenclass/128267/3 "2008-02-06T12:28:09Z")

</div>

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

Cheers,  
Alex

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [February 6, 2008, 1:01pm UTC](https://www.ruby-forum.com/t/data-for-eigenclass/128267/4 "2008-02-06T13:01:30Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [February 6, 2008, 2:44pm UTC](https://www.ruby-forum.com/t/data-for-eigenclass/128267/5 "2008-02-06T14:44:35Z")

</div>

> > I am not sure if I understand you. Class variables(@@bar) are  
> > available for its children:  
> > No longer in Ruby 1.9  
> > [eigenclass.org](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](http://radarek.jogger.pl) - mój blog

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [February 6, 2008, 1:09pm UTC](https://www.ruby-forum.com/t/data-for-eigenclass/128267/6 "2008-02-06T13:09:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [February 6, 2008, 4:17pm UTC](https://www.ruby-forum.com/t/data-for-eigenclass/128267/7 "2008-02-06T16:17:31Z")

</div>

2008/2/6 Rados³aw Bu³at [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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
