[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Data for eigenclass

Jari Williamsson

2/6/2008 9:06:00 AM

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

6 Answers

Thomas Wieczorek

2/6/2008 9:20:00 AM

0

On Wed, Feb 6, 2008 at 10:05 AM, Jari Williamsson
<jari.williamsson@mailbox.swipnet.se> 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!"

Alexandru E. Ungur

2/6/2008 11:27:00 AM

0

>>> 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
> <jari.williamsson@mailbox.swipnet.se> 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+Ru...

Cheers,
Alex

ThoML

2/6/2008 11:59:00 AM

0

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

Jari Williamsson

2/6/2008 12:08:00 PM

0

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

Radoslaw Bulat

2/6/2008 1:44:00 PM

0

PiA+IEkgYW0gbm90IHN1cmUgaWYgSSB1bmRlcnN0YW5kIHlvdS4gQ2xhc3MgdmFyaWFibGVzKEBA
YmFyKSBhcmUKPiA+IGF2YWlsYWJsZSBmb3IgaXRzIGNoaWxkcmVuOgo+IE5vIGxvbmdlciBpbiBS
dWJ5IDEuOQo+IGh0dHA6Ly9laWdlbmNsYXNzLm9yZy9oaWtpL0NoYW5nZXMraW4rUnVieSsxLjkj
bDM4CgpEaWQgeW91IHRyeT8gQWN0dWFsbHkgZWlnZW5jbGFzcyBpcyBub3QgdXAgdG8gZGF0ZS4g
TWF0eiBjaGFuZ2VkIGFuZApsYXRlciByb2xsZWQgYmFjayBtYW55IHRoaW5ncy4gSXQgd29ya3Mg
Zm9yIG1lLCBzbyBJIGd1ZXNzIGl0J3Mgb25lIG9mCnRoZXNlIHRoaW5ncy4KCiQgcnVieTEuOSAt
dgpydWJ5IDEuOS4wICgyMDA4LTAxLTE0IHJldmlzaW9uIDApIFtpNjg2LWxpbnV4XQokIHJ1Ynkx
LjkgLWUgImNsYXNzIEE7IEBAYSA9IDE7IGVuZDsgY2xhc3MgQiA8IEE7IEBAYSBlbmQiCgotLSAK
UmFkb3OzYXcgQnWzYXQKCmh0dHA6Ly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