From: "\"dooby\" , co.uk@... Date: 2003-01-16T11:20:47+09:00 Subject: Re: trouble understanding const_set() "Chris Pine" wrote in message news:132b01c2bb42$737ea330$6401a8c0@MELONBALLER... > Hello, > > I'm trying to understand why the following code is giving me an error. It > seems like it isn't finding Foo::Constant (which doesn't exist), but why > isn't it looking for Bar::Constant, instead? > > As you can see, `self' is Bar, not Foo: > > > irb(main):001:0> class Foo > irb(main):002:1> def Foo.foo > irb(main):003:2> const_set 'Constant', 'just a string' > irb(main):004:2> puts self.inspect ># irb(main):005:2> puts Constant irb(main):005:2> puts const_get('Constant') > irb(main):006:2> end > irb(main):007:1> end > nil > irb(main):008:0> class Bar < Foo > irb(main):009:1> end > nil > irb(main):010:0> Bar.foo Bar just a string nil The change to line 5 averts the [uninitialized constant] error. Your example shows the answer to a possible supplementary question: "Must Bar.const_get('Constant') be used thereafter ?" > irb(main):011:0> Bar::Constant > "just a string" > .... happily, no. daz