From: Josh Cheek Date: 2011-08-01T04:50:30+09:00 Subject: Re: Documentation in Ruby --90e6ba48856d8e4e5104a962d35e Content-Type: text/plain; charset=ISO-8859-1 On Sun, Jul 31, 2011 at 3:28 AM, amir e. wrote: > Hi > I have a problem with Ruby Reference. How can I see all the methods of a > class? for example from where I know that array has a method that its > name is "detect" ? > > thanks > > -- > Posted via http://www.ruby-forum.com/. > > There are several useful methods: obj.methods # public methods obj.public_methods obj.private_methods obj.protected_methods obj.singleton_methods Class.instance_methods Class.instance_methods(false) # no inherited methods Note that array arithmetic can sometimes be useful, ie: module M def m end end class C include M def c end end C.instance_methods - Object.new.methods # => [:c, :m] Also note that grepping can sometimes be useful, ie: Array.new.methods.grep(/index/) # => [:each_index, :find_index, :index, :rindex, :each_with_index] Also, if you're doing this in IRB, consider trying Pry, which offers the `ls` command that makes this sort of thing less tedious. I give a few examples of it in my screencast @ http://vimeo.com/26391171 here's all the options it supports: pry(main)> ls -h Usage: ls [OPTIONS] [VAR] List information about VAR (the current context by default). Shows local and instance variables by default. -- -g, --globals Display global variables. -c, --constants Display constants. -l, --locals Display locals. -i, --ivars Display instance variables. -k, --class-vars Display class variables. -m, --methods Display methods (public methods by default). -M, --instance-methods Display instance methods (only relevant to classes and modules). -P, --public Display public methods (with -m). -r, --protected Display protected methods (with -m). -p, --private Display private methods (with -m). -j, --just-singletons Display just the singleton methods (with -m). -s, --super Include superclass entries excluding Object (relevant to constant and methods options). -e, --everything Include superclass entries including Object (must be combined with -s switch). -a, --all Display all types of entries. -v, --verbose Verbose ouput. -f, --flood Do not use a pager to view text longer than one screen. --grep REG Regular expression to be used. -h, --help Show this message. On Sun, Jul 31, 2011 at 7:24 AM, 7stud -- wrote: > > All the methods that an object can call come from several sources: > > all it's parent classes > > Presumably you mean classes inherited from its class and singleton class? > modules its parent class's include > > Presumably you mean classes its singleton class includes? If one knows enough internals, the list can be made elegant: its class and classes its class inherits from. But for that, you have to understand how Ruby lies to you in order to have singleton classes and look like multiple inheritance. If anyone is interested in that, there is a fantastic blog entry that covers most of it @ http://carboni.ca/blog/p/Modules-How-Do-They-Work. --90e6ba48856d8e4e5104a962d35e--