Is there a way to view a method's source code from the Rails console?

ruby-on-rails, ruby-on-rails-3

Solution

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 --

Problem

Let's say I have the following class: ``` class User < ActiveRecord::Base def fullname "#{self.first_name} #{self.last_name}" end end ``` Is it possible for me to go into the console and view the fullname method's source code output in the console somehow? Like, it would look like... ``` irb(main):010:0> omg_console_you_are_awesome_show_source(User.fullname) [Fri Jun 29 14:11:31 -0400 2012] => def fullname [Fri Jun 29 14:11:31 -0400 2012] => "#{self.first_name} #{self.last_name}" [Fri Jun 29 14:11:31 -0400 2012] => end ``` Or really any way to view source code? Thanks!

Original source