Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View available methods ruby

how can I view all the available methods on an object in ruby. I'm using the aptana IDE when I type File. no methods are displayed. I'm coming from an eclipse/java background.

Thanks

like image 750
user231413 Avatar asked Dec 14 '09 16:12

user231413


People also ask

Does Ruby have methods or functions?

Ruby doesn't really have functions. Rather, it has two slightly different concepts - methods and Procs (which are, as we have seen, simply what other languages call function objects, or functors). Both are blocks of code - methods are bound to Objects, and Procs are bound to the local variables in scope.

What is Respond_to in Ruby?

respond_to is a Rails method for responding to particular request types. For example: def index @people = Person.find(:all) respond_to do |format| format.html format.xml { render :xml => @people.to_xml } end end.

How do you define a class method in Ruby?

There are two standard approaches for defining class method in Ruby. The first one is the “def self. method” (let's call it Style #1), and the second one is the “class << self” (let's call it Style #2). Both of them have pros and cons.


2 Answers

There are several methods:

obj.methods
obj.public_methods
obj.private_methods
obj.protected_methods
obj.singleton_methods

Update

  1. To get the object methods apart from all inherited methods you can do:

    obj.methods(false)

  2. As Tempus mentioned in the comments, the following command is very helpful to get the current object methods apart from the Object(base class) inherited methods:

    obj.methods - Object.methods

like image 170
khelll Avatar answered Sep 28 '22 08:09

khelll


You can pass true to the methods if you want to ignore the methods defined in superclasses:

obj.methods(true)
obj.public_methods(true)
obj.private_methods(true)
obj.protected_methods(true)
obj.singleton_methods(true)

Or, if you only want to remove the most common methods that are defined in the Object class, you want to append either - Object.methods or - Object.instance_methods, depending on whether obj is a class or an instance of a class.

like image 21
sarahhodne Avatar answered Sep 28 '22 07:09

sarahhodne