Access to classes and modules from the Ruby standard library can be obtained in a BasicObject subclass by referencing the desired constant from the root like ::File or ::Enumerator. Like 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) end end
--
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 rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/groups/opt_out.
I believe the culprit here is Delegator.Delegator owns const_missing, it is all or nothing (reproduced from memory):
but it should not in my view: after trying its heuristic it should delegate to super if defined in case there are more heuristics above. Something like this (off the top of my head):
The key question to address this today is: Which is the public API of AS::Dependencies? Can I include ModuleConstMissing in the singleton class of my class? Can I call load_missing_constant?The answer is no, those belong to the implementation of AS::Dependencies. The public interface is pretty small, autoload_paths, require_dependency, and some other stuff, not much. Dependencies.rb is mostly about its contract.So as of today, I'd say the best solution is the one based on require_depedency. That is using public interface, and looks clean in your source code compared to overriding const_missing. If the code was mine I would add a comment to explain the call.
Perhaps AS could provide a subclass of SimpleDelegator that does basically what I wrote above. But that's in a way duplicating (and thus depending) on the implementation of const_missing in Delegator today, is monkey patching in disguise in my opinion. Not really convinced.
This is a corner-case though. For this problem to happen we need a class that inherits from SimpleDelegator and in addition acts as a namespace. Not sure if it deserves reopening a class of stdlib to "fix it".
All taken into account, I believe this should be considered a gotcha of the combination SimpleDelegator + namespace + autoloading. And perhaps Delegator could be patched in future versions of Ruby.
On Sun, Mar 2, 2014 at 8:11 PM, Xavier Noria <f...@hashref.com> wrote:I believe the culprit here is Delegator.Delegator owns const_missing, it is all or nothing (reproduced from memory):but it should not in my view: after trying its heuristic it should delegate to super if defined in case there are more heuristics above. Something like this (off the top of my head):
On Sun, Mar 2, 2014 at 10:20 PM, Henrik Nyh <hen...@nyh.se> wrote:On Sun, Mar 2, 2014 at 8:11 PM, Xavier Noria <f...@hashref.com> wrote:I believe the culprit here is Delegator.Delegator owns const_missing, it is all or nothing (reproduced from memory):but it should not in my view: after trying its heuristic it should delegate to super if defined in case there are more heuristics above. Something like this (off the top of my head):This small experiment illustrates that your solution does the trick: https://gist.github.com/henrik/9314943
Just a remark about this comment:"Load stdlib classes even though SimpleDelegator inherits from BasicObject."The purpose of that const_get is to load top-level constants (which belong to Object), regardless of whether they come from stdlib.