From: Peter McLain Date: 2010-03-04T06:57:27+09:00 Subject: Re: Class vs Module in constant lookup On Mar 3, 2010, at 12:15 PM, Caleb Clausen wrote: > On 3/3/10, Peter McLain wrote: >> >> Constant lookup behaves differently if there is a class versus a >> module in the name: > > Extending your example a little to actually look up the constants > instead of just checking if they're defined?: > > XYZ = 10 > > module M > end > > class C > end > > p defined? M::XYZ # => nil > p defined? C::XYZ # => "constant" > > C::XYZ > #(irb):13: warning: toplevel constant XYZ referenced by C::XYZ > #=> 10 > > M::XYZ > # raises NameError > > > Note the warning for C::XYZ. You really shouldn't look up constants > this way, so I think a little inconsistency like this is tolerable. Well, the code isn't mine, but I'd still like to understand if there is a rationale for the difference in behavior, or if this is just another MRI implementation detail that's leaked out. Currently, MagLev returns "constant" for both classes and modules, but that breaks third party code. E.g,: return unless defined? RDoc::VERSION # RDoc 1 does not have VERSION # code that blows up if RDoc 1 being used... One could argue that the proper test, which works for both classes and modules in all ruby implementations, is: return unless RDoc.const_defined? :VERSION But I'd still like to understand if the difference in behavior is intended or an implementation quirk, and if it is intended, what the rationale is. -- Peter McLain peter.mclain@gemstone.com