Professional Web Applications Themes

accessing constants from class methods - Ruby

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

  1. #1

    Default accessing constants from class methods


    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 Guest

  2. #2

    Default Re: accessing constants from class methods


    "Ara.T.Howard" wrote: 

    #-----

    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 Guest

  3. #3

    Default Re: accessing constants from class methods

    On Sat, 14 Feb 2004, daz wrote:
     
    >
    > #-----
    >
    > 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[/ref]

    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 Guest

  4. #4

    Default Re: accessing constants from class methods

    David A. Black wrote:
     

    The origional code defines x as

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

    while daz's code says

    def x
    const_defined?(:X) ? const_get(:X) : nil
    end

    it uses const_defined and const_get

    Best Regards

    Mark Sparshatt




    Mark Guest

  5. #5

    Default Re: accessing constants from class methods

     
    >[/ref]

    Your post didn't get through to the NG, David.
    Ara probably didn't see it and there appeared to be no replies
    when I posted.

    http://groups.google.com/groups?hl=en&th=5677a0c1234ad853


    I'm wary about the defined? keyword -- I can't find a good
    definition of what service it provides.


    for i in 0..1 # (avoiding new scope)
    puts defined? a
    a = 1
    puts defined? a
    puts '-' * 10
    end

    #-> nil
    #-> local-variable
    #-> ----------
    #-> nil
    #-> local-variable
    #-> ----------

    ## Strangely, for constants it works differently.

    for i in 0..1
    puts defined? X
    X = 1
    puts defined? X
    puts '-' * 10
    end

    #-> nil
    #-> constant
    #-> ----------
    #-> constant
    #-> constant
    #-> ----------

    ## The next test made me wonder if I can believe anything, any more.

    for i in 0..1
    puts defined? Z
    const_set Z, 1
    puts defined? Z
    puts '-' * 10
    end

    #-> nil
    #-> C:/TEMP/rb3273.TMP:17: uninitialized constant Z (NameError)
    #-> from C:/TEMP/rb3273.TMP:15:in `each'
    #-> from C:/TEMP/rb3273.TMP:15

    =begin
    --------------------------------------------------------------- Module#const_set
    mod.const_set( aSymbol, anObject ) -> anObject
    --------------------------------------------------------------------------------
    Sets the named constant to the given object, returning that object.
    Creates a new constant if no constant with the given name previously existed.
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^
    [...]
    =end


    daz



    daz Guest

  6. #6

    Default Re: accessing constants from class methods

    daz wrote: 

    The first argument to #const_set should be a symbol or string, and the
    receiver of #const_set should be a module.

    for i in 0..1
    puts defined? Z
    Object::const_set :Z, 1
    puts defined? Z
    puts '-' * 10
    end

    #-> nil
    #-> constant
    #-> ----------
    #-> constant
    #-> constant
    #-> ----------

    Keep believing...


    Joel Guest

  7. #7

    Default Re: accessing constants from class methods


    "Ara.T.Howard" <gov> schrieb im Newsbeitrag
    news:ngdc.noaa.gov... 
    the 
    return it" 
    'X'... 

    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 Guest

  8. #8

    Default Re: accessing constants from class methods

    On Mon, 16 Feb 2004, daz wrote:
     
    > >[/ref]
    >
    > Your post didn't get through to the NG, David.
    > Ara probably didn't see it and there appeared to be no replies
    > when I posted.
    >
    > http://groups.google.com/groups?hl=en&th=5677a0c1234ad853[/ref]

    yes exactly - sorry david.
     


    THIS is the error i was getting but couldn't seem to reproduce it in response
    to an offline question from david. now i see that i could have done something
    very subtle to (re)create the bug...

    thanks for the warning - i will be catious when using 'defined?' in the
    future.

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

  9. #9

    Default Re: accessing constants from class methods


    "Joel VanderWerf" wrote: 
    >
    > The first argument to #const_set should be a symbol or string, and the
    > receiver of #const_set should be a module.
    >
    > [...]
    >
    > Keep believing...
    >[/ref]

    Faith restored, once again :)

    Cheers,


    daz



    daz Guest

Similar Threads

  1. [PHP-DEV] Accessing child constants from base class
    By Dan Cox in forum PHP Development
    Replies: 15
    Last Post: October 11th, 02:30 PM
  2. Instance- and class-variables (was mixing in class methods)
    By dblack@superlink.net in forum Ruby
    Replies: 2
    Last Post: October 3rd, 03:52 AM
  3. [PHP-DEV] class constants bugs
    By Cristiano Duarte in forum PHP Development
    Replies: 2
    Last Post: September 30th, 10:51 PM
  4. PHP5 class constants
    By Cristiano Duarte in forum PHP Development
    Replies: 0
    Last Post: August 10th, 02:27 AM
  5. Accessing constants from another package/file.
    By ed in forum PERL Miscellaneous
    Replies: 5
    Last Post: June 26th, 01:24 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139