accessing constants from class methods

Discussion in 'Ruby' started by Ara.T.Howard, Feb 13, 2004.

  1. Ara.T.Howard

    Ara.T.Howard Guest

    what i would like to accomplish would be something like this:


    class Abstract
    class << self
    def x
    # defined?(self::X) ? self::X : nil
    end
    end
    end

    class Concrete < Abstract
    X = 42
    end

    # p Base.x # => nil
    # p Derived.x # => 42


    does this make sense? in essence i want an abstract class method with the
    semantics of "iff a concrete class has defined a certain constant, return it"

    the problem is that 'self::X' above tries to reference 'Class::X', if i
    replace 'self' with 'Abstract' then i will be referencing the incorrect 'X'...

    how does one say 'the constant of the current class' from within a class
    method?

    -a
    --
    ===============================================================================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
    | URL :: http://www.ngdc.noaa.gov/stp/
    | TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done
    ===============================================================================
     
    Ara.T.Howard, Feb 13, 2004
    #1
    1. Advertising

  2. Ara.T.Howard

    daz Guest

    "Ara.T.Howard" wrote:
    >
    > what i would like to accomplish would be something like this:
    >
    >
    > class Abstract
    > class << self
    > def x
    > # defined?(self::X) ? self::X : nil
    > end
    > end
    > end
    >
    > class Concrete < Abstract
    > X = 42
    > end
    >
    > # p Base.x # => nil
    > # p Derived.x # => 42
    >
    >
    > does this make sense? in essence i want an abstract class method with the
    > semantics of "iff a concrete class has defined a certain constant, return it"
    >
    > the problem is that 'self::X' above tries to reference 'Class::X', if i
    > replace 'self' with 'Abstract' then i will be referencing the incorrect 'X'...
    >
    > how does one say 'the constant of the current class' from within a class
    > method?
    >


    #-----

    class A
    class << self
    def x
    const_defined?:)X) ? const_get:)X) : nil
    end
    end
    end

    class C < A
    X = 42
    end

    p A.x # => nil
    p C.x # => 42

    #-----


    daz
     
    daz, Feb 14, 2004
    #2
    1. Advertising

  3. Ara.T.Howard

    Ara.T.Howard Guest

    On Sat, 14 Feb 2004, daz wrote:

    > Date: Sat, 14 Feb 2004 08:24:08 -0000
    > From: daz <>
    > Newsgroups: comp.lang.ruby
    > Subject: Re: accessing constants from class methods
    >
    >
    > "Ara.T.Howard" wrote:
    > >
    > > what i would like to accomplish would be something like this:
    > >
    > >
    > > class Abstract
    > > class << self
    > > def x
    > > # defined?(self::X) ? self::X : nil
    > > end
    > > end
    > > end
    > >
    > > class Concrete < Abstract
    > > X = 42
    > > end
    > >
    > > # p Base.x # => nil
    > > # p Derived.x # => 42
    > >
    > >
    > > does this make sense? in essence i want an abstract class method with the
    > > semantics of "iff a concrete class has defined a certain constant, return it"
    > >
    > > the problem is that 'self::X' above tries to reference 'Class::X', if i
    > > replace 'self' with 'Abstract' then i will be referencing the incorrect 'X'...
    > >
    > > how does one say 'the constant of the current class' from within a class
    > > method?
    > >

    >
    > #-----
    >
    > class A
    > class << self
    > def x
    > const_defined?:)X) ? const_get:)X) : nil
    > end
    > end
    > end
    >
    > class C < A
    > X = 42
    > end
    >
    > p A.x # => nil
    > p C.x # => 42
    >
    > #-----
    >
    >
    > daz


    perfect!

    -a
    --
    ===============================================================================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
    | URL :: http://www.ngdc.noaa.gov/stp/
    | TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done
    ===============================================================================
     
    Ara.T.Howard, Feb 14, 2004
    #3
  4. "Ara.T.Howard" <> schrieb im Newsbeitrag
    news:p...
    >
    > what i would like to accomplish would be something like this:
    >
    >
    > class Abstract
    > class << self
    > def x
    > # defined?(self::X) ? self::X : nil
    > end
    > end
    > end
    >
    > class Concrete < Abstract
    > X = 42
    > end
    >
    > # p Base.x # => nil
    > # p Derived.x # => 42
    >
    >
    > does this make sense? in essence i want an abstract class method with

    the
    > semantics of "iff a concrete class has defined a certain constant,

    return it"
    >
    > the problem is that 'self::X' above tries to reference 'Class::X', if i
    > replace 'self' with 'Abstract' then i will be referencing the incorrect

    'X'...
    >
    > how does one say 'the constant of the current class' from within a class
    > method?


    Use instance vars of classes:

    class Class
    def x
    ancestors.each do |cl|
    val = cl.instance_variable_get("@x")
    return val if val
    end

    nil
    end

    def x=(val); @x=val; end
    end

    class Abstract; end

    class Concrete < Abstract
    self.x = 42
    end

    class MostConcrete < Concrete
    self.x = "forty two"
    end

    p Abstract.x
    p Concrete.x
    p MostConcrete.x

    Regards

    robert
     
    Robert Klemme, Feb 16, 2004
    #4
    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. Oltmans
    Replies:
    6
    Views:
    354
    Terry Reedy
    Mar 11, 2009
  2. Jos Backus
    Replies:
    6
    Views:
    203
    Jos Backus
    Dec 2, 2004
  3. Csaba Henk
    Replies:
    6
    Views:
    135
  4. Martin DeMello
    Replies:
    3
    Views:
    122
    Joel VanderWerf
    Nov 9, 2006
  5. Kenneth McDonald
    Replies:
    5
    Views:
    326
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page