From: Andreas Launila Date: 2007-06-12T04:15:39+09:00 Subject: Re: Constants and metaclasses Joel VanderWerf wrote: > Andreas Launila wrote: >> The line "f.bar" will work fine, but "f.foo" will fail with "NameError: >> uninitialized constant Foo::BAR". Is there some way to have the method >> defined in the original class pick up on the constant in the metaclass >> (without altering "p BAR")? >> > > Yes, with a little wrangling: > > class Object > def singleton_class > class << self; self; end > end > end > > class Foo > def foo > p self.singleton_class::BAR > end > end > > Yes that works, but I'm specifically looking for something that does not alter the line "p BAR". To get around that one would have to dynamically set the constant BAR in the main class before executing "p BAR". A problem with that comes when one has multiple instances and metaclasses, one would end up changing the value of a constant each time the method is invoked. Thanks for the reply though. -- Andreas Launila