Google Groups Home
Help | Sign in
get modules that are in a class?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Aaron Smith  
View profile
 More options Jul 7 2007, 10:00 pm
Newsgroups: comp.lang.ruby
From: Aaron Smith <beingthexempl...@gmail.com>
Date: Sun, 8 Jul 2007 11:00:55 +0900
Local: Sat, Jul 7 2007 10:00 pm
Subject: get modules that are in a class?
is it possible to find out what modules have been included inside of a
class?

--
Posted via http://www.ruby-forum.com/.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Shea  
View profile
 More options Jul 7 2007, 10:40 pm
Newsgroups: comp.lang.ruby
From: Chris Shea <cms...@gmail.com>
Date: Sun, 08 Jul 2007 02:40:11 -0000
Local: Sat, Jul 7 2007 10:40 pm
Subject: Re: get modules that are in a class?
On Jul 7, 8:00 pm, Aaron Smith <beingthexempl...@gmail.com> wrote:

> is it possible to find out what modules have been included inside of a
> class?

> --
> Posted viahttp://www.ruby-forum.com/.

This appears to do the trick:

mvb:~ cms$ irb
001:0> class C
002:1> end
nil
003:0> C.ancestors.select {|a| a.class == Module}
[Wirble::Shortcuts, PP::ObjectMixin, Kernel]

You can tell I require wirble and pp in my irbrc.

HTH,
Chris


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Aaron Smith  
View profile
 More options Jul 7 2007, 10:47 pm
Newsgroups: comp.lang.ruby
From: Aaron Smith <beingthexempl...@gmail.com>
Date: Sun, 8 Jul 2007 11:47:23 +0900
Local: Sat, Jul 7 2007 10:47 pm
Subject: Re: get modules that are in a class?

Wayne E. Seguin wrote:
> On Jul 07, 2007, at 22:00 , Aaron Smith wrote:
>> is it possible to find out what modules have been included inside of a
>> class?

> Yes Aaron it is, via the method "included_modules"

> http://www.ruby-doc.org/core/classes/Module.html#M001697

What about within a class?

module TestModule
  def say_something
    puts "SOMETHING"
  end
end

class Test
  include TestModule
end

t = Test.new
puts t.included_modules

--
Posted via http://www.ruby-forum.com/.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Travis D Warlick Jr  
View profile
 More options Jul 7 2007, 11:10 pm
Newsgroups: comp.lang.ruby
From: Travis D Warlick Jr <warli...@operissystems.com>
Date: Sun, 8 Jul 2007 12:10:00 +0900
Subject: Re: get modules that are in a class?

Remember that when you include the module, you're including _all_ the
module's methods (including Module#included_modules)

So, use self.class to get the Class object of the current instance (this
will work from the included modules also), so you should be able to do:

self.class.included_modules

And a debugging efficiency tip: use the Array#sort method with the
Module#included_modules to sort the list of included modules for easier
viewing.  I use this all the time in IRB. (This also works with
Class#methods and all the like)

irb(main):001:0> YourClass.methods.sort

--
  Travis Warlick
  Operis Systems, LLC
  Lead Developer


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Aaron Smith  
View profile
 More options Jul 7 2007, 11:15 pm
Newsgroups: comp.lang.ruby
From: Aaron Smith <beingthexempl...@gmail.com>
Date: Sun, 8 Jul 2007 12:15:56 +0900
Local: Sat, Jul 7 2007 11:15 pm
Subject: Re: get modules that are in a class?

> And a debugging efficiency tip: use the Array#sort method with the
> Module#included_modules to sort the list of included modules for easier
> viewing.  I use this all the time in IRB. (This also works with
> Class#methods and all the like)

> irb(main):001:0> YourClass.methods.sort

Thanks. That's perfect.

--
Posted via http://www.ruby-forum.com/.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
dbl...@wobblini.net  
View profile
 More options Jul 8 2007, 7:32 am
Newsgroups: comp.lang.ruby
From: dbl...@wobblini.net
Date: Sun, 8 Jul 2007 20:32:49 +0900
Local: Sun, Jul 8 2007 7:32 am
Subject: Re: get modules that are in a class?
Hi --

On Sun, 8 Jul 2007, Travis D Warlick Jr wrote:

It's not exactly an inclusion thing.  Class objects already respond to
#included_modules, because Class inherits from Module.

> So, use self.class to get the Class object of the current instance (this
> will work from the included modules also), so you should be able to do:

> self.class.included_modules

That will work with any object:

   "".class.included_modules

etc.  It's not dependent on your having included a module.

David

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
   RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
     & consulting:  Ruby Power and Light, LLC (http://www.rubypal.com)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
vasudevram  
View profile
 More options Jul 8 2007, 12:54 pm
Newsgroups: comp.lang.ruby
From: vasudevram <vasudev...@gmail.com>
Date: Sun, 08 Jul 2007 16:54:38 -0000
Local: Sun, Jul 8 2007 12:54 pm
Subject: Re: get modules that are in a class?

>YourClass.methods.sort

Yes, that's a useful trick. I use it all the time.
We can also make up many more such, with a bit of thought.

Another one I use a lot, when I think that some class is likely to
have a method with some  substring  in its name, is:

YourClass.methods.grep /substring/

e.g. : String.methods.grep /case/ # to find out what the String method
name to uppercase (or lowercase) a string, is called.
or
"".methods.grep /case/

Vasudev Ram
http://www.dancingbison.com
http://jugad.livejournal.com
http://sourceforge.net/projects/xtopdf


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google