advice requested / attributes.rb

Discussion in 'Ruby' started by Ara.T.Howard, Apr 29, 2005.

  1. Ara.T.Howard

    Ara.T.Howard Guest

    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::x
    p A::y
    p A::z

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

    p C::x
    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
    ===============================================================================
     
    Ara.T.Howard, Apr 29, 2005
    #1
    1. Advertising

  2. Ara.T.Howard

    Dave Burt Guest

    "Ara.T.Howard" <> 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
     
    Dave Burt, Apr 30, 2005
    #2
    1. Advertising

  3. Ara.T.Howard

    Guest

    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::x #=> 42.0

    but instead you get

    p A::x #=> 42
    p B::x #=> 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::x #=> nil

    but instead you get

    p B::x #=> 42

    and then if you

    class C < B; end

    B::x = 42.0

    you'll have

    p A::x #=> 42
    p B::x #=> 42.0
    p C::x #=> 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
    ===============================================================================
     
    , Apr 30, 2005
    #3
    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. Brian
    Replies:
    2
    Views:
    2,161
    BMukes
    May 28, 2004
  2. Skeets

    Guru Advice Requested...

    Skeets, Feb 17, 2006, in forum: XML
    Replies:
    3
    Views:
    391
    Martin Honnen
    Feb 19, 2006
  3. David Straley

    Advice on architecture requested

    David Straley, Jul 23, 2003, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    122
    David Straley
    Jul 23, 2003
  4. middletree

    programming advice requested

    middletree, Jan 2, 2004, in forum: ASP General
    Replies:
    2
    Views:
    154
    middletree
    Jan 3, 2004
  5. Alex Martelli

    performance and style advice requested

    Alex Martelli, Sep 14, 2003, in forum: Ruby
    Replies:
    12
    Views:
    201
    Alex Martelli
    Sep 16, 2003
Loading...

Share This Page