Data for eigenclass

Discussion in 'Ruby' started by Jari Williamsson, Feb 6, 2008.

  1. 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 Williamsson
     
    Jari Williamsson, Feb 6, 2008
    #1
    1. Advertising

  2. On Wed, Feb 6, 2008 at 10:05 AM, Jari Williamsson
    <> 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!"
     
    Thomas Wieczorek, Feb 6, 2008
    #2
    1. Advertising

  3. >>> sender: "Thomas Wieczorek" date: "Wed, Feb 06, 2008 at 06:19:53PM +0900" <<<EOQ
    > On Wed, Feb 6, 2008 at 10:05 AM, Jari Williamsson
    > <> 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:

    No longer in Ruby 1.9
    http://eigenclass.org/hiki/Changes in Ruby 1.9#l38

    Cheers,
    Alex
     
    Alexandru E. Ungur, Feb 6, 2008
    #3
  4. Jari Williamsson

    tho_mica_l Guest

    > 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.
     
    tho_mica_l, Feb 6, 2008
    #4
  5. tho_mica_l wrote:
    >> 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.


    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 Williamsson
     
    Jari Williamsson, Feb 6, 2008
    #5
  6. PiA+IEkgYW0gbm90IHN1cmUgaWYgSSB1bmRlcnN0YW5kIHlvdS4gQ2xhc3MgdmFyaWFibGVzKEBA
    YmFyKSBhcmUKPiA+IGF2YWlsYWJsZSBmb3IgaXRzIGNoaWxkcmVuOgo+IE5vIGxvbmdlciBpbiBS
    dWJ5IDEuOQo+IGh0dHA6Ly9laWdlbmNsYXNzLm9yZy9oaWtpL0NoYW5nZXMraW4rUnVieSsxLjkj
    bDM4CgpEaWQgeW91IHRyeT8gQWN0dWFsbHkgZWlnZW5jbGFzcyBpcyBub3QgdXAgdG8gZGF0ZS4g
    TWF0eiBjaGFuZ2VkIGFuZApsYXRlciByb2xsZWQgYmFjayBtYW55IHRoaW5ncy4gSXQgd29ya3Mg
    Zm9yIG1lLCBzbyBJIGd1ZXNzIGl0J3Mgb25lIG9mCnRoZXNlIHRoaW5ncy4KCiQgcnVieTEuOSAt
    dgpydWJ5IDEuOS4wICgyMDA4LTAxLTE0IHJldmlzaW9uIDApIFtpNjg2LWxpbnV4XQokIHJ1Ynkx
    LjkgLWUgImNsYXNzIEE7IEBAYSA9IDE7IGVuZDsgY2xhc3MgQiA8IEE7IEBAYSBlbmQiCgotLSAK
    UmFkb3OzYXcgQnWzYXQKCmh0dHA6Ly9yYWRhcmVrLmpvZ2dlci5wbCAtIG3zaiBibG9nCg==
     
    Rados³aw Bu³at, Feb 6, 2008
    #6
  7. MjAwOC8yLzYgUmFkb3OzYXcgQnWzYXQgPHJhZGVrLmJ1bGF0QGdtYWlsLmNvbT46Cj4KPiAgRGlk
    IHlvdSB0cnk/IEFjdHVhbGx5IGVpZ2VuY2xhc3MgaXMgbm90IHVwIHRvIGRhdGUuIE1hdHogY2hh
    bmdlZCBhbmQKPiAgbGF0ZXIgcm9sbGVkIGJhY2sgbWFueSB0aGluZ3MuIEl0IHdvcmtzIGZvciBt
    ZSwgc28gSSBndWVzcyBpdCdzIG9uZSBvZgo+ICB0aGVzZSB0aGluZ3MuCj4KPiAgJCBydWJ5MS45
    IC12Cj4gIHJ1YnkgMS45LjAgKDIwMDgtMDEtMTQgcmV2aXNpb24gMCkgW2k2ODYtbGludXhdCj4g
    ICQgcnVieTEuOSAtZSAiY2xhc3MgQTsgQEBhID0gMTsgZW5kOyBjbGFzcyBCIDwgQTsgQEBhIGVu
    ZCIKPgoKSXQgd29ya3MgaW4gMS44LjYgZm9yIG1lOgoKQzpcPnJ1YnkgLXYKcnVieSAxLjguNiAo
    MjAwNy0wMy0xMyBwYXRjaGxldmVsIDApIFtpMzg2LW1zd2luMzJdCgpDOlw+cnVieSAtZSAiY2xh
    c3MgQTsgQEBhPTE7IHB1dHMgQEBhOyBlbmQ7IGNsYXNzIEIgPCBBOyBwdXRzIEBAYSBlbmQiCjEK
    MQo=
     
    Thomas Wieczorek, Feb 6, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Trans
    Replies:
    19
    Views:
    193
    Trans
    Mar 15, 2006
  2. Matt Todd

    Defining Eigenclass

    Matt Todd, Aug 11, 2006, in forum: Ruby
    Replies:
    20
    Views:
    347
    Logan Capaldo
    Aug 13, 2006
  3. Mauricio Fernandez
    Replies:
    0
    Views:
    181
    Mauricio Fernandez
    Jan 5, 2007
  4. Paolo Nusco Perrotta

    Superclass of eigenclass in 1.8.6

    Paolo Nusco Perrotta, Apr 16, 2007, in forum: Ruby
    Replies:
    0
    Views:
    101
    Paolo Nusco Perrotta
    Apr 16, 2007
  5. Wolfgang Nádasi-donner

    Typos in eigenclass - Changes in Ruby 1.9

    Wolfgang Nádasi-donner, Aug 3, 2007, in forum: Ruby
    Replies:
    0
    Views:
    140
    Wolfgang Nádasi-donner
    Aug 3, 2007
Loading...

Share This Page