Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding out where methods are defined in Ruby/Rails (as opposed to Java)

Tags:

I am just getting started with Ruby on Rails. Coming from the Java world, one thing that I am wondering is how do Ruby/Rails developers find out where methods are actually defined.

I am used to just clicking on the method in Eclipse to find where is is defined even in third party libraries (supposing I have the source code).

A concrete example: I am trying to find out how the Authlogic gem apparently changes the constructor of my User class to require an additional parameter (called :password_confirmation) even though the User class doesn't even inherit from anything related to Authlogic.

Probably I am just overlooking something really obvious here (or maybe I still can't wrap my head around the whole "convention over configuration" thing ;-))

like image 819
Hendrik Avatar asked Aug 23 '11 09:08

Hendrik


People also ask

How do you check if a method is defined in Ruby?

When a variable is undefined, it means that it contains no value. We can check if a value is defined or undefined in Ruby using the local_variables. include?(:) method.

What is call method in Rails?

It is used to call back a piece of code that has been passed around as an object.


1 Answers

It's slightly difficult to quickly find the method location for dynamic languages like Ruby.

You can use object.methods or object.instance_methods to quickly find out the methods.

If you are using Ruby 1.9, you can do something like this:

object.method(:method_name).source_location  

For more information on source_location - click here

like image 192
Arun Kumar Arjunan Avatar answered Oct 03 '22 08:10

Arun Kumar Arjunan