# Question about looking up classes

**URL:** <https://www.ruby-forum.com/t/question-about-looking-up-classes/116371>\
**Category:** Ruby\
**Created:** [October 4, 2007, 11:10pm UTC](https://www.ruby-forum.com/t/question-about-looking-up-classes/116371 "2007-10-04T23:10:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BallS\_Donald\_A\_Jr\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/da6949/32.png) [@BallS\_Donald\_A\_Jr\_S](https://www.ruby-forum.com/u/BallS_Donald_A_Jr_S)\
**Post date:** [October 4, 2007, 11:10pm UTC](https://www.ruby-forum.com/t/question-about-looking-up-classes/116371/1 "2007-10-04T23:10:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BallS\_Donald\_A\_Jr\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/da6949/32.png) [@BallS\_Donald\_A\_Jr\_S](https://www.ruby-forum.com/u/BallS_Donald_A_Jr_S)\
**Post date:** [October 5, 2007, 2:04pm UTC](https://www.ruby-forum.com/t/question-about-looking-up-classes/116371/2 "2007-10-05T14:04:07Z")

</div>

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

…  
def recognize\_path(path)  
::ActionController::Routing…  
end  
…

---

<div class="post-metadata">

**Author:** ![BallS\_Donald\_A\_Jr\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/da6949/32.png) [@BallS\_Donald\_A\_Jr\_S](https://www.ruby-forum.com/u/BallS_Donald_A_Jr_S)\
**Post date:** [October 5, 2007, 5:25pm UTC](https://www.ruby-forum.com/t/question-about-looking-up-classes/116371/3 "2007-10-05T17:25:55Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![BallS\_Donald\_A\_Jr\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/da6949/32.png) [@BallS\_Donald\_A\_Jr\_S](https://www.ruby-forum.com/u/BallS_Donald_A_Jr_S)\
**Post date:** [October 5, 2007, 2:10pm UTC](https://www.ruby-forum.com/t/question-about-looking-up-classes/116371/4 "2007-10-05T14:10:33Z")

</div>

Upon second reading, I believe I misread the question?
