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 ;-))
The Pry gem is designed precisely for this kind of explorative use-case.
Pry is an interactive shell that lets you navigate your way around a program’s source-code using shell-like commands such as
cdandls.You can pull the documentation for any method you encounter and even view the source code, including the native C code in some cases (with the
pry-docplugin). You can even jump directly to the file/line where a particular method is defined with theedit-methodcommand. Theshow-methodandshow-doccommands also display the precise location of the method they’re acting on.Watch the railscast screencast for more information.
Here are some examples below:
You can also look up sourcecode with the
show-methodcommand:See http://pry.github.com for more information 🙂