Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > advice requested / attributes.rb

Reply
Thread Tools

advice requested / attributes.rb

 
 
Ara.T.Howard
Guest
Posts: n/a
 
      04-29-2005


i just made a mod to my attributes module which allows this

jib:~/eg/ruby/attributes/attributes-1.1.1 > cat a.rb
require './lib/attributes-1.1.1'

class A
class_attributes %w( x y z )
x 4
y 1
z 1
end

class B < A
y 2
end

class C < B
z 2
end

p A:
p A::y
p A::z

p B:
p B::y
p B::z

p C:
p C::y
p C::z

jib:~/eg/ruby/attributes/attributes-1.1.1 > ruby a.rb
4
1
1
4
2
1
4
2
2


so, as you can see these class attributes are 'inherited' instance values.
this is done using class instance vars and a search up the ancestors list if an
attribute has not been set (defined?) in a class.

questions :

- does this sit o.k. with people : does it violate pols? i think it's quite
natural but that's me

- what do i call it? for now i'm calling it an class 'inherited' variable
since it's not really a class instance (@) var nor a hierarchy (@@) var.

comments?

-a
--
================================================== =============================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| renunciation is not getting rid of the things of this world, but accepting
| that they pass away. --aitken roshi
================================================== =============================

 
Reply With Quote
 
 
 
 
Dave Burt
Guest
Posts: n/a
 
      04-30-2005
"Ara.T.Howard" <(E-Mail Removed)> asked:
>
> class A
> class_attributes %w( x y z )
> x 4
> y 1
> z 1
> end
>
> class B < A
> y 2
> end
> <snip>
> so, as you can see these class attributes are 'inherited' instance values.
> this is done using class instance vars and a search up the ancestors list
> if an
> attribute has not been set (defined?) in a class.
>
> questions :
>
> - does this sit o.k. with people : does it violate pols? i think it's
> quite
> natural but that's me


POLS belongs to Matz, doesn't it?

> - what do i call it? for now i'm calling it an class 'inherited'
> variable
> since it's not really a class instance (@) var nor a hierarchy (@@)
> var.


I don't see that it's clear that these are not @@ variables - wouldn't that
be the obvious implementation for that behaviour, and for something called
"class attributes"?

Cheers,
Dave


 
Reply With Quote
 
 
 
 
Ara.T.Howard@noaa.gov
Guest
Posts: n/a
 
      04-30-2005
On Sat, 30 Apr 2005, Dave Burt wrote:

> I don't see that it's clear that these are not @@ variables - wouldn't that
> be the obvious implementation for that behaviour, and for something called
> "class attributes"?


if it WERE simply an @@var you'd have

class A
class_attribute 'x'
x 42
end

class B < A
x 42.0
end

p A: #=> 42.0

but instead you get

p A: #=> 42
p B: #=> 42.0

if it were simply a class @var you'd have

class A
class_attribute 'x'
x 42
end

class B < A
end

p B: #=> nil

but instead you get

p B: #=> 42

and then if you

class C < B; end

B: = 42.0

you'll have

p A: #=> 42
p B: #=> 42.0
p C: #=> 42.0

make sense? you __inherit__ the 'x'. with @vars every class has one and
there is no sharing. with @@vars there is ONE and ONLY ONE - all classes in a
hierarchy share it. this works using follow logic:

- if you set the var you have it
- else look up ancestry chain for it

cheers.

-a
--
================================================== =============================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| renunciation is not getting rid of the things of this world, but accepting
| that they pass away. --aitken roshi
================================================== =============================

 
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
Guru Advice Requested... Skeets XML 3 02-19-2006 11:39 AM
Uninstalling Problems. Advice Requested, Please Robert11 Computer Support 1 06-11-2004 09:21 PM
Server Control Collection Properties Solutions,Problems MVP Advice Requested Brian ASP .Net 2 05-28-2004 02:26 PM
MP3 player advice requested - please read inside Chocolate Sauce Computer Support 8 07-05-2003 01:12 PM
MP3 player advice requested - please read inside Chocolate Sauce Computer Support 1 07-04-2003 11:15 AM



Advertisments