我在玩弄 pry ,撞到了让我困惑的东西。
irb
pry
cd Array
pry(Array):>
show-method
返回:
def pretty_print ...
def pretty_print_cycle ...
那些方法在哪里?我从 Ruby Array doc 查看了 ruby 数组并且找不到方法。我去了search for array in github ruby project看到 Array.c 但找不到 Array.rb。即使那样,我也无法在 Array.c 中找到 pretty_print。
谁能帮我理解一下?
谢谢!
最佳答案
试试这个:
[].method(:pretty_print).source_location
=> ["/home/joel/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/pp.rb", 328]
似乎 show-method 给出了同样的信息:
show-method [].pretty_print
From: /home/joel/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/pp.rb @ line 328:
Owner: Array
Visibility: public
Number of lines: 7
def pretty_print(q)
q.group(1, '[', ']') {
q.seplist(self) {|v|
q.pp v
}
}
end
pry.rb 需要 pp:
https://github.com/pry/pry/blob/dd2d9b927b0afbe4e5463ff1558c566abbdb0473/lib/pry.rb
关于c - 如果我在Ruby中 pry Array,那么pretty_print函数从哪里来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24945647/