accessing constants from class methods

A

Ara.T.Howard

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
===============================================================================
 
D

daz

Ara.T.Howard said:
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
 
A

Ara.T.Howard

Date: Sat, 14 Feb 2004 08:24:08 -0000
From: daz <[email protected]>
Newsgroups: comp.lang.ruby
Subject: Re: accessing constants from class methods




#-----

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
===============================================================================
 
R

Robert Klemme

Ara.T.Howard said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

inheriting constants 2
inheritence of class vars 2
Class::name 0
Object#copy [rcr?] 4
narray on windows? 1
parent of TrueClass, FalseClass 9
pretty exceptions 0
idiot's guide to druby using ssh tunnels 4

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top