如何使Irb不打印对象属性
我想irb默认不打印我的对象属性。 那可能吗? 例如,我有:
class Test @tmp = nil def initialize(str) @tmp = str self end end
我得到:
irb> t = Test.new("hello") => #
我不想看到@tmp="hello"
部分。
IRB调用默认的返回值。 如果你想在IRB中压制或更改输出,只需在你的class级中覆盖inspect
。
class Test def initialize(str) @tmp = str end def inspect '' end end test = Test.new('foobar') #=>
设置检查器字符串,如下所示:
IRB.CurrentContext.inspect_mode = IRB::Inspector.new(->obj{ "<##{obj.class}:#{obj.object_id}>" })
然后在irb中,您会得到以下内容:
"foo" # => <#String:47128160661240>