Question about looking up classes

Hi folks. I’ve run into some behavior in a rails app that I don’t
understand. I don’t think it’s specific to rails, but feel free to
redirect me if I’m off-topic.

I wanted to manually invoke the rails router path recognition in one of
my controller classes. When I tried the naive solution:

def index

result_params =
ActionController::Routing::Routes.recognize_path(uri.path)

end

I got a NameError:

uninitialized constant
ActionWebService::Dispatcher::ActionController::Routing

I google codesearched for anyone else doing this sort of thing and
stumbled across the solution of putting the call into a metaclass
method:

class << self
def recognize_path(path)
ActionController::Routing::Routes.recognize_path(path)
end
end

def index

result_params = self.class.recognize_path(uri.path)

end

I’m quite puzzled as to why the class lookup works in the context of a
metaclass method, but doesn’t in the context of an instance method (or a
normal class method, for that matter), but am not sure if I’m running
into rails metamagic or my own incomplete understanding of ruby. Any
tips for me?

  • donald

I believe that is is a scope issue.
Try explicitely specifying the global scope using “::” like so:


def recognize_path(path)
::ActionController::Routing…
end

I believe that is is a scope issue.
Try explicitely specifying the global scope using “::” like so:


def recognize_path(path)
::ActionController::Routing…
end

Just so. Thanks, I had heretofore been that prepending :: forced global
scope.

  • donald

Upon second reading, I believe I misread the question?