On Wed, 16 Nov 2005, Nicholas Seckar wrote:

> The loaded file is expected to define the constant; We assume b.rb contains
> class or module B (or defines it in any other way). If the constant isn't
> defined by the loaded file then a NameError is raised as if normal.

i understand that.  but in your original message you said

> However, let's say we're slightly misguided and do MyClass::MyModule. Since
> this is not defined, our const_missing handler is once again called.
> 'my_module.rb' is thus loaded again, (usually to no effect,) and MyModule
> returned.
>
> This is a pretty clear violation of the semantics of ::. Indeed, Ruby will
> produce a warning regarding our misbehaved const_missing. That said, there
> does not seem to be a way to behave correctly; from inside the const_missing
> handler there is no way to tell which case the constant is missing in.

here you will see that wether we reference B from inside M, or as M::B the
handler is called exactly once.

   harp:~ > cat a.rb
   class Module
     def const_missing c
       puts "const_missing called once..."
       const_set c, 42
     end
   end

   module A
     p B
   end

   p A::B


   harp:~ > ruby a.rb
   const_missing called once...
   42
   42

here is another example which loads a file

   harp:~ > cat a.rb
   class Module
     def const_missing c
       puts "const_missing called once..."
       autoload = {
         :B => 'b.rb'
       }
       autoload[c] ? require(autoload[c]) : super
       const_set(c, const_get(c))
     end
   end

   module A
     B
   end

   A::B

   module A; p B; end
   p A::B

   harp:~ > ruby a.rb
   const_missing called once...
   42
   42

so what __exactly__ are you trying to do in your handler that does not work?  i
understand you problem but, as the second example shows, it can easily be
solved without distinguishing the two syntax cases because, afaikt, ruby
handles them precisely the same.

hth.

-a
-- 
===============================================================================
| ara [dot] t [dot] howard [at] gmail [dot] com
| all happiness comes from the desire for others to be happy.  all misery
| comes from the desire for oneself to be happy.
| -- bodhicaryavatara
===============================================================================