撬检查方法不起作用
pry inspect method not working
我从 理解计算 一书中获得了以下代码。目的是改变 inspect
行为。
class Number < Struct.new(:value)
def inspect
"<<#{self}>>"
end
def to_s
value.to_s
end
end
当我使用 irb
:
时,它按预期工作
irb(main):014:0> Number.new(1)
=> <<1>>
但当我使用 pry
:
时却没有
[8] pry(main)> n = Number.new(1)
=> #<struct Number value=1>
Pry 是 Ruby 2.0.0 上的 0.10.3 版。为什么它不起作用?
Pry 不只是使用 inspect
来显示 return 值。它调用在配置中定义的名为 print object 的过程。在lib/pry.rb
中可以发现设置为:
class Pry
# The default print
DEFAULT_PRINT = proc do |output, value, _pry_|
_pry_.pager.open do |pager|
pager.print _pry_.config.output_prefix
Pry::ColorPrinter.pp(value, pager, Pry::Terminal.width! - 1)
end
end
end
为了像irb
那样使用inspect
,请按照说明here:
这样设置
Pry.config.print = proc {|output, value| output.puts "=> #{value.inspect}"}
那么您将获得:
pry(main)> n = Number.new(1)
=> <<1>>
我使用 Pry
版本 0.10.4
。
我刚刚在我的 .pryrc
文件中添加了以下几行(我认为,那是 good
此类代码的位置):
if defined?(BigDecimal)
BigDecimal.class_eval do
def inspect
"<#{to_s('+3F')}>"
end
end
end
结果:
balance: <+100.0>,
commission_amount: <+0.15>
Sawa 是正确的,因为 Pry 使用它自己的打印机,但是如果你仔细观察 source you can see that it actually uses Ruby's PP
behind the scenes, and PP
defines its own behaviour 漂亮的打印结构:
class Struct # :nodoc:
def pretty_print(q) # :nodoc:
q.group(1, sprintf("#<struct %s", PP.mcall(self, Kernel, :class).name), '>') {
q.seplist(PP.mcall(self, Struct, :members), lambda { q.text "," }) {|member|
q.breakable
q.text member.to_s
q.text '='
q.group(1) {
q.breakable ''
q.pp self[member]
}
}
}
end
def pretty_print_cycle(q) # :nodoc:
q.text sprintf("#<struct %s:...>", PP.mcall(self, Kernel, :class).name)
end
end
如果您有兴趣了解更多相关信息,请查看 documentation(尽管内容很简短)。
所以在你的结构中你也可以定义你自己的 pretty_print
和 pretty_print_cycle
方法,这意味着 Pry 可以按照你想要的方式打印这些,而不必覆盖它们的 DEFAULT_PRINT
proc .
我从 理解计算 一书中获得了以下代码。目的是改变 inspect
行为。
class Number < Struct.new(:value)
def inspect
"<<#{self}>>"
end
def to_s
value.to_s
end
end
当我使用 irb
:
irb(main):014:0> Number.new(1)
=> <<1>>
但当我使用 pry
:
[8] pry(main)> n = Number.new(1)
=> #<struct Number value=1>
Pry 是 Ruby 2.0.0 上的 0.10.3 版。为什么它不起作用?
Pry 不只是使用 inspect
来显示 return 值。它调用在配置中定义的名为 print object 的过程。在lib/pry.rb
中可以发现设置为:
class Pry
# The default print
DEFAULT_PRINT = proc do |output, value, _pry_|
_pry_.pager.open do |pager|
pager.print _pry_.config.output_prefix
Pry::ColorPrinter.pp(value, pager, Pry::Terminal.width! - 1)
end
end
end
为了像irb
那样使用inspect
,请按照说明here:
Pry.config.print = proc {|output, value| output.puts "=> #{value.inspect}"}
那么您将获得:
pry(main)> n = Number.new(1)
=> <<1>>
我使用 Pry
版本 0.10.4
。
我刚刚在我的 .pryrc
文件中添加了以下几行(我认为,那是 good
此类代码的位置):
if defined?(BigDecimal)
BigDecimal.class_eval do
def inspect
"<#{to_s('+3F')}>"
end
end
end
结果:
balance: <+100.0>,
commission_amount: <+0.15>
Sawa 是正确的,因为 Pry 使用它自己的打印机,但是如果你仔细观察 source you can see that it actually uses Ruby's PP
behind the scenes, and PP
defines its own behaviour 漂亮的打印结构:
class Struct # :nodoc:
def pretty_print(q) # :nodoc:
q.group(1, sprintf("#<struct %s", PP.mcall(self, Kernel, :class).name), '>') {
q.seplist(PP.mcall(self, Struct, :members), lambda { q.text "," }) {|member|
q.breakable
q.text member.to_s
q.text '='
q.group(1) {
q.breakable ''
q.pp self[member]
}
}
}
end
def pretty_print_cycle(q) # :nodoc:
q.text sprintf("#<struct %s:...>", PP.mcall(self, Kernel, :class).name)
end
end
如果您有兴趣了解更多相关信息,请查看 documentation(尽管内容很简短)。
所以在你的结构中你也可以定义你自己的 pretty_print
和 pretty_print_cycle
方法,这意味着 Pry 可以按照你想要的方式打印这些,而不必覆盖它们的 DEFAULT_PRINT
proc .