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
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
“amir e.” [email protected] writes:
I have a problem with Ruby Reference. How can I see all the methods of a
class?
Use ‘Class#instance_methods’ method. ‘Array.instance_methods’,
for.example. By
default it returns all class’ instance methods, if you pass ‘false’ as
an argument to it - only those defined in class itself, without
ancestors’
amir e. wrote in post #1013953:
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
All the methods that an object can call come from several sources:
its singleton class
its class
all its parent classes
modules its class includes
modules its parent class’s include
One way to find ‘detect’ is to go to the docs for Array, and when you
don’t see the method listed there, look under the heading Included
Modules. In this case, Enumerable is listed, and if you click on that
link, you will be taken to the Enumerable class and the methods listed
there include detect.
On Sun, Jul 31, 2011 at 09:24:46PM +0900, 7stud – wrote:
modules its parent class’s include
What does this mean?
Chad P. wrote in post #1013996:
On Sun, Jul 31, 2011 at 09:24:46PM +0900, 7stud – wrote:
modules its parent class’s include
What does this mean?
s/class’s/classes’/gc
On 07/31/2011 01: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
If you look at http://rdoc.info/stdlib/core/1.9.2/Array you will see
“detect” listed under “Methods included from Enumerable”.
-Justin
7stud: you forgot some source
Modules which are extended to the object or Modules which are included
in the objects singleton class
On Sun, Jul 31, 2011 at 3:28 AM, amir e. [email protected] wrote:
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 @ Pry Screencast on Vimeo here’s all
the
options it supports:
-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 – [email protected]
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs