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 :: Solar-Terrestrial Physics Data | NCEI
TRY :: for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done
===============================================================================
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
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
Date: Sat, 14 Feb 2004 08:24:08 -0000
From: daz dooby@d10.karoo.co.uk
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
–
EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
URL :: Solar-Terrestrial Physics Data | NCEI
TRY :: for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done
===============================================================================
Date: Sat, 14 Feb 2004 08:24:08 -0000
From: daz dooby@d10.karoo.co.uk
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!
OK, it’s probably something simple but I’m staring and staring at the
original code, and its output (which is the same as daz’s code), and I
can’t figure out why it wasn’t OK.
OK, it’s probably something simple but I’m staring and staring at the
original code, and its output (which is the same as daz’s code), and I
can’t figure out why it wasn’t OK.
Explanations welcome
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
OK, it’s probably something simple but I’m staring and staring at the
original code, and its output (which is the same as daz’s code), and I
can’t figure out why it wasn’t OK.
Explanations welcome
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
Yes, that much I saw What I didn’t get was what the problem
was to begin with. self::X looked to me like it worked fine.
OK, it’s probably something simple but I’m staring and staring at the
original code, and its output (which is the same as daz’s code), and I
can’t figure out why it wasn’t OK.
Explanations welcome
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.
Sets the named constant to the given object, returning that object.
Creates a new constant if no constant with the given name previously existed.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[...]
Date: Mon, 16 Feb 2004 06:55:25 -0000
From: daz dooby@d10.karoo.co.uk
Newsgroups: comp.lang.ruby
Subject: Re: accessing constants from class methods
David A. Black wrote:
OK, it’s probably something simple but I’m staring and staring at the
original code, and its output (which is the same as daz’s code), and I
can’t figure out why it wasn’t OK.
Explanations welcome
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.
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
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.