Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Data for eigenclass

Reply
Thread Tools

Data for eigenclass

 
 
Jari Williamsson
Guest
Posts: n/a
 
      02-06-2008
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

 
Reply With Quote
 
 
 
 
Thomas Wieczorek
Guest
Posts: n/a
 
      02-06-2008
On Wed, Feb 6, 2008 at 10:05 AM, Jari Williamsson
<(E-Mail Removed)> 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!"

 
Reply With Quote
 
 
 
 
Alexandru E. Ungur
Guest
Posts: n/a
 
      02-06-2008
>>> 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
> <(E-Mail Removed)> 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

 
Reply With Quote
 
tho_mica_l
Guest
Posts: n/a
 
      02-06-2008
> 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.
 
Reply With Quote
 
Jari Williamsson
Guest
Posts: n/a
 
      02-06-2008
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

 
Reply With Quote
 
Radosław Bułat
Guest
Posts: n/a
 
      02-06-2008
PiA+IEkgYW0gbm90IHN1cmUgaWYgSSB1bmRlcnN0YW5kIHlvdS 4gQ2xhc3MgdmFyaWFibGVzKEBA
YmFyKSBhcmUKPiA+IGF2YWlsYWJsZSBmb3IgaXRzIGNoaWxkcm VuOgo+IE5vIGxvbmdlciBpbiBS
dWJ5IDEuOQo+IGh0dHA6Ly9laWdlbmNsYXNzLm9yZy9oaWtpL0 NoYW5nZXMraW4rUnVieSsxLjkj
bDM4CgpEaWQgeW91IHRyeT8gQWN0dWFsbHkgZWlnZW5jbGFzcy BpcyBub3QgdXAgdG8gZGF0ZS4g
TWF0eiBjaGFuZ2VkIGFuZApsYXRlciByb2xsZWQgYmFjayBtYW 55IHRoaW5ncy4gSXQgd29ya3Mg
Zm9yIG1lLCBzbyBJIGd1ZXNzIGl0J3Mgb25lIG9mCnRoZXNlIH RoaW5ncy4KCiQgcnVieTEuOSAt
dgpydWJ5IDEuOS4wICgyMDA4LTAxLTE0IHJldmlzaW9uIDApIF tpNjg2LWxpbnV4XQokIHJ1Ynkx
LjkgLWUgImNsYXNzIEE7IEBAYSA9IDE7IGVuZDsgY2xhc3MgQi A8IEE7IEBAYSBlbmQiCgotLSAK
UmFkb3OzYXcgQnWzYXQKCmh0dHA6Ly9yYWRhcmVrLmpvZ2dlci 5wbCAtIG3zaiBibG9nCg==

 
Reply With Quote
 
Thomas Wieczorek
Guest
Posts: n/a
 
      02-06-2008
MjAwOC8yLzYgUmFkb3OzYXcgQnWzYXQgPHJhZGVrLmJ1bGF0QG dtYWlsLmNvbT46Cj4KPiAgRGlk
IHlvdSB0cnk/IEFjdHVhbGx5IGVpZ2VuY2xhc3MgaXMgbm90IHVwIHRvIGRhdG UuIE1hdHogY2hh
bmdlZCBhbmQKPiAgbGF0ZXIgcm9sbGVkIGJhY2sgbWFueSB0aG luZ3MuIEl0IHdvcmtzIGZvciBt
ZSwgc28gSSBndWVzcyBpdCdzIG9uZSBvZgo+ICB0aGVzZSB0aG luZ3MuCj4KPiAgJCBydWJ5MS45
IC12Cj4gIHJ1YnkgMS45LjAgKDIwMDgtMDEtMTQgcmV2aXNpb2 4gMCkgW2k2ODYtbGludXhdCj4g
ICQgcnVieTEuOSAtZSAiY2xhc3MgQTsgQEBhID0gMTsgZW5kOy BjbGFzcyBCIDwgQTsgQEBhIGVu
ZCIKPgoKSXQgd29ya3MgaW4gMS44LjYgZm9yIG1lOgoKQzpcPn J1YnkgLXYKcnVieSAxLjguNiAo
MjAwNy0wMy0xMyBwYXRjaGxldmVsIDApIFtpMzg2LW1zd2luMz JdCgpDOlw+cnVieSAtZSAiY2xh
c3MgQTsgQEBhPTE7IHB1dHMgQEBhOyBlbmQ7IGNsYXNzIEIgPC BBOyBwdXRzIEBAYSBlbmQiCjEK
MQo=

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Typos in eigenclass - Changes in Ruby 1.9 Wolfgang NĂĄdasi-donner Ruby 0 08-03-2007 08:23 PM
Superclass of eigenclass in 1.8.6 Paolo Nusco Perrotta Ruby 0 04-16-2007 09:50 PM
mini-contest: eigenclass.org "Happy 2007!" Ruby challenge (2007-01-06 20H UTC) Mauricio Fernandez Ruby 0 01-05-2007 12:51 PM
Defining Eigenclass Matt Todd Ruby 20 08-13-2006 04:57 AM
Alternate notation for eigenclass Trans Ruby 19 03-15-2006 02:09 PM



Advertisments