From: dblack@... Date: 2007-06-12T04:38:49+09:00 Subject: Re: Constants and metaclasses Hi -- On Tue, 12 Jun 2007, Andreas Launila wrote: > Is there a way to define a constant in a metaclass and then have it used > in methods defined in the original class? > > An example: > > > class Foo > def foo > p BAR > end > end > > f = Foo.new > class < BAR = "Hello" > > def bar > p BAR > end > end > > f.bar > f.foo > > > 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")? I don't think so (and I hope not, as it would be kind of strange :-) but you could define BAR in the original class: class << f Foo::BAR = "Hello" etc. David -- * Books: RAILS ROUTING (new! http://safari.awprofessional.com/9780321509246) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)