From: "David A. Black" Date: 2004-02-14T11:09:04+09:00 Subject: Re: accessing constants from class methods Hi -- On Sat, 14 Feb 2004, 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 ^^^^^^^ you mean Abstract/Concrete, I assume :-) I think this already does what you want, doesn't it? $ cat ara.rb class Abstract class << self def x defined?(self::X) ? self::X : nil end end end class Concrete < Abstract X = 42 end p Abstract.x p Concrete.x dblack@laptop:~/hacking$ ruby -v ara.rb ruby 1.6.8 (2002-12-24) [i686-linux-gnu] nil 42 on the general principle that self will get set to the receiver (Concrete, in this case) -- so, for example: irb(main):001:0> class A; def A.x; p self; end; end => nil irb(main):002:0> class B < A; end => nil irb(main):003:0> B.x B (Or did I misunderstand?) David -- David A. Black dblack@wobblini.net