# Given an object, how to find public methods in included modules and the parent module?

**URL:** <https://rubytalk.org/t/given-an-object-how-to-find-public-methods-in-included-modules-and-the-parent-module/36334>\
**Category:** ruby-talk\
**Created:** [21 March 2007 16:03 UTC](https://rubytalk.org/t/given-an-object-how-to-find-public-methods-in-included-modules-and-the-parent-module/36334 "2007-03-21T16:03:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Danielbuus](https://avatars.discourse-cdn.com/v4/letter/d/df788c/32.png) [@Danielbuus](https://rubytalk.org/u/Danielbuus)\
**Post date:** [21 March 2007 16:03 UTC](https://rubytalk.org/t/given-an-object-how-to-find-public-methods-in-included-modules-and-the-parent-module/36334/1 "2007-03-21T16:03:25Z")

</div>

Hey 🙂

I'm trying to extend a method for creating a site map for a Ruby on  
Rails app. This question, I believe, is pretty much just Ruby though,  
that's why I'm asking here.

Basically, what it does ATM is find all classes that end in  
'\_controller.rb' and require them,

&nbsp;&nbsp;require 'find'

&nbsp;&nbsp;# Require all controllers  
&nbsp;&nbsp;Find.find(File.join(RAILS\_ROOT, "app/controllers")) { |path|  
require\_dependency path if path =~ /\_controller\.rb$/ }

Okay. So I have two issues, and therefore two questions for this:

a) The "app/controllers" path does not just contain files, it also  
contains directories, each of which present a module, like "Manage::"  
for the backend, and "Store::" for the frontend. Inside are class  
files that share names, e.g. 'Store::Items' and 'Manage::Items'. Only  
difference is the module they belong to.

b) In the backend classes, I include a module, 'include Backend::Crud'  
which defines some shared methods on the backend classes.

My question for a) is, how can I distinguish between the different  
classes, given that they belong to different modules. How can I ask an  
object what module it belongs to?

And, b), how can I look up these included methods? Right now, I can  
only find public methods that are in the class files themselves, not  
those defined in included modules...

I'm a bit confused with all this, it's pretty low-level for me 😉 So I  
apologize if I'm asking very stupidly. If you have any ideas of how I  
could do this, please let me know.

Thanks in advance,  
Daniel

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [21 March 2007 16:33 UTC](https://rubytalk.org/t/given-an-object-how-to-find-public-methods-in-included-modules-and-the-parent-module/36334/2 "2007-03-21T16:33:47Z")

</div>

can you start out by telling us \_\_what\_\_ you are trying to accomplish?

-a

> **···**
>
> On Thu, 22 Mar 2007 danielbuus@gmail.com wrote:
> 
> > Hey 🙂
> > 
> > I'm trying to extend a method for creating a site map for a Ruby on  
> > Rails app. This question, I believe, is pretty much just Ruby though,  
> > that's why I'm asking here.
> > 
> > Basically, what it does ATM is find all classes that end in  
> > '\_controller.rb' and require them,
> > 
> > require 'find'
> > 
> > # Require all controllers  
> > Find.find(File.join(RAILS\_ROOT, "app/controllers")) { |path|  
> > require\_dependency path if path =~ /\_controller\.rb$/ }
> > 
> > Okay. So I have two issues, and therefore two questions for this:
> > 
> > a) The "app/controllers" path does not just contain files, it also  
> > contains directories, each of which present a module, like "Manage::"  
> > for the backend, and "Store::" for the frontend. Inside are class  
> > files that share names, e.g. 'Store::Items' and 'Manage::Items'. Only  
> > difference is the module they belong to.
> > 
> > b) In the backend classes, I include a module, 'include Backend::Crud'  
> > which defines some shared methods on the backend classes.
> > 
> > My question for a) is, how can I distinguish between the different  
> > classes, given that they belong to different modules. How can I ask an  
> > object what module it belongs to?
> > 
> > And, b), how can I look up these included methods? Right now, I can  
> > only find public methods that are in the class files themselves, not  
> > those defined in included modules...
> > 
> > I'm a bit confused with all this, it's pretty low-level for me 😉 So I  
> > apologize if I'm asking very stupidly. If you have any ideas of how I  
> > could do this, please let me know.
> > 
> > Thanks in advance,  
> > Daniel
> 
> --  
> be kind whenever possible... it is always possible.  
> - the dalai lama

---

<div class="post-metadata">

**Author:** ![Danielbuus](https://avatars.discourse-cdn.com/v4/letter/d/df788c/32.png) [@Danielbuus](https://rubytalk.org/u/Danielbuus)\
**Post date:** [21 March 2007 17:25 UTC](https://rubytalk.org/t/given-an-object-how-to-find-public-methods-in-included-modules-and-the-parent-module/36334/3 "2007-03-21T17:25:10Z")

</div>

> can you start out by telling us \_\_what\_\_ you are trying to accomplish?

Yes 🙂 I'm trying to basically "load in" a bunch of class files and  
detect their public methods, including those defined in included  
modules. Consider this file structure:

apps/  
&nbsp;&nbsp;controllers/  
&nbsp;&nbsp;&nbsp;&nbsp;store/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;items\_controller.rb -\> class Store::ItemsController \<\<  
ApplicationController  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def index  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# blah blah  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;manage/  
&nb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;end  
lib/  
&nbsp;&nbsp;poly.rb -\> module Poly  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;module BackendCrud  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def save  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# blah blah  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

Okay. Given that file tree, I want to "examine" all controllers in app/  
controllers/, so that I can produce something like this:  
{ 'store' =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'items' =\> [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'index'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},  
&nbsp;&nbsp;'manage' =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'items' =\> [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'show',  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'save'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
}

Do you know what I mean?

Cheers,  
Daniel 🙂

> **···**
>
> On Mar 21, 5:33 pm, ara.t.how...@noaa.gov wrote:
> 
> > -a  
> > --  
> > be kind whenever possible... it is always possible.  
> > - the dalai lama
