Orl*_*ndo 10 ruby-on-rails ruby-on-rails-3
假设我有以下课程:
class User < ActiveRecord::Base
  def fullname
    "#{self.first_name} #{self.last_name}"
  end
end
我可以进入控制台并以某种方式在控制台中查看fullname方法的源代码输出吗?就像,它看起来像......
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
或者真的以任何方式查看源代码?谢谢!
Yla*_*n S 24
你也可以使用pry (http://pry.github.com/)就像IRB一样使用类固醇.你可以这样做:
[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 --
| 归档时间: | 
 | 
| 查看次数: | 6995 次 | 
| 最近记录: |