Is there a way to view a method's source code from the Rails console?
You can also use pry (http://pry.github.com/) which is like IRB on steroids. You can do stuff like:
[1] pry(main)> show-source Array#each
From: array.c in Ruby Core (C Method):
Number of lines: 11
Owner: Array
Visibility: public
VALUE
rb_ary_each(VALUE ary)
{
long i;
RETURN_ENUMERATOR(ary, 0, 0);
for (i=0; i<RARRAY_LEN(ary); i++) {
rb_yield(RARRAY_PTR(ary)[i]);
}
return ary;
}
[2] pry(main)> show-doc Array#each
From: array.c in Ruby Core (C Method):
Number of lines: 11
Owner: Array
Visibility: public
Signature: each()
Calls block once for each element in self, passing that
element as a parameter.
If no block is given, an enumerator is returned instead.
a = [ "a", "b", "c" ]
a.each {|x| print x, " -- " }
produces:
a -- b -- c --
Not exactly what you are asking, but this Railscast might help.
It teaches you a trick that will allow you to open the method in your text editor from the Rails console.
UPDATE:
I just realized that link is behind a paywall... Here's a summary of the trick.
Add this to your ~/.irbrc file
class Object
def mate(method_name)
file, line = method(method_name).source_location
`mate '#{file}' -l #{line}`
end
end
...where mate is the CLI command to open TextMate (of course subl could be used here for Sublime Text).
Then in the console simply call
helper.mate(:number_to_currency)
...where number_to_currency is the method who's source you want to view.
BTW, if you don't already, you should subscribe to Railscast Pro. IMO, there is not a better way to spend 9 dollars per month. And to disclose, I have no relationship with that site other then being a satisfied customer.