I modified my blog post to implement a RailsySimpleDelegator that appears
to handle both stdlib constant lookup and Rails autoloading:
http://thepugautomatic.com/2014/03/simpledelegator-autoloading-issues-with-rails/

Maybe it would be the lesser of two evils to have Rails monkeypatch
SimpleDelegator with that implementation of const_missing. The other evil
being to let this gotcha remain.


On Sun, Mar 2, 2014 at 6:14 PM, Henrik Nyh <[email protected]> wrote:

> Oh, good call with BasicObject. I see its docs (
> http://www.ruby-doc.org/core-2.1.1/BasicObject.html) mention this exact
> case:
>
> Access to classes and modules from the Ruby standard library can be
> obtained in a 
> BasicObject<http://www.ruby-doc.org/core-2.1.1/BasicObject.html> subclass
> by referencing the desired constant from the root like ::File or
> ::Enumerator. Like 
> method_missing<http://www.ruby-doc.org/core-2.1.1/BasicObject.html#method-i-method_missing>,
> const_missing can be used to delegate constant lookup to Object:
>
> class MyObjectSystem < BasicObject
>   def self.const_missing(name)
>     ::Object.const_get(name)
>   endend
>
>
>
> On Sun, Mar 2, 2014 at 10:15 AM, Xavier Noria <[email protected]> wrote:
>
>> Interesting.
>>
>> The problem is that the const_missing in Delegator issues
>> ::Object.const_get by hand. Essentially this is the situation (modulus I
>> still need coffee :):
>>
>>   class Object
>>     def self.const_missing(cname)
>>       1
>>     end
>>   end
>>
>>   class C < BasicObject
>>     def self.const_missing(cname)
>>       ::Object.const_get(cname)
>>     end
>>   end
>>
>>   C::A # => 1
>>
>> The problem there is that Delegator effectively throws the namespace by
>> calling const_get on Object.
>>
>> To have the constant autoloaded MyThing.const_missing should be invoked
>> instead for Rails to know it should look into that namespace as one of
>> possible candidates (it can because self.name returns "MyThing").
>>
>> In general, to play by the rules const_missing in a class should call
>> super if if is not able to find the constant, but Delegate cannot assume a
>> lot since it inherits from BasicObject. On the other hand I don't quite see
>> the point in that implementation, doing Object.const_get sounds quite
>> arbitrary to me at first sight, doesn't make a lot of sense to me.
>>
>> I don't have a workaround to propose right now, let me think about it,
>> but that's more or less the situation.
>>
>>  --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "Ruby on Rails: Core" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/rubyonrails-core/PjGUK72BmFA/unsubscribe
>> .
>> To unsubscribe from this group and all its topics, send an email to
>> [email protected].
>> To post to this group, send email to [email protected].
>> Visit this group at http://groups.google.com/group/rubyonrails-core.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to