当我对内置类进行子类化时,为什么检查中的行为会发生变化.但是,当我将自定义子类化为子类时,却看不到.
class MainError end class AnotherTestError < StandardError def initialize @label_test = "hey!" end end class TestError < MainError def initialize @label_test = "hey!" end end a = AnotherTestError.new puts a.inspect # output: #<AnotherTestError: AnotherTestError> t = TestError.new puts t.inspect # output: #<TestError:0x007f99e12409f0 @label_test="hey!">
解决方法
因为很多(大多数?全部?)内置类
>用C编写,和
> #incpect覆盖.
例如,Exception
(StandardError的超类)定义#inspect如下:
exc_inspect(VALUE exc) { VALUE str,klass; klass = CLASS_OF(exc); exc = rb_obj_as_string(exc); if (RSTRING_LEN(exc) == 0) { return rb_str_dup(rb_class_name(klass)); } str = rb_str_buf_new2("#<"); klass = rb_class_name(klass); rb_str_buf_append(str,klass); rb_str_buf_cat(str,": ",2); rb_str_buf_append(str,exc); rb_str_buf_cat(str,">",1); return str; }
有趣的是构建返回字符串.
Object#inspect
,另一方面定义:
static VALUE rb_obj_inspect(VALUE obj) { if (rb_ivar_count(obj) > 0) { VALUE str; VALUE c = rb_class_name(CLASS_OF(obj)); str = rb_sprintf("-<%"PRIsVALUE":%p",c,(void*)obj); return rb_exec_recursive(inspect_obj,obj,str); } else { return rb_any_to_s(obj); } }
它递归地包含具有名称和值的实例变量.