Feature #6130 » 0001-object.c-rb_obj_inspect-Kernel-inspect-improve-consi.patch
bignum.c | ||
---|---|---|
rb_cBignum = rb_define_class("Bignum", rb_cInteger);
|
||
rb_define_method(rb_cBignum, "to_s", rb_big_to_s, -1);
|
||
rb_define_alias(rb_cBignum, "inspect", "to_s");
|
||
rb_define_method(rb_cBignum, "coerce", rb_big_coerce, 1);
|
||
rb_define_method(rb_cBignum, "-@", rb_big_uminus, 0);
|
||
rb_define_method(rb_cBignum, "+", rb_big_plus, 1);
|
io.c | ||
---|---|---|
rb_define_method(rb_cARGF, "initialize", argf_initialize, -2);
|
||
rb_define_method(rb_cARGF, "initialize_copy", argf_initialize_copy, 1);
|
||
rb_define_method(rb_cARGF, "to_s", argf_to_s, 0);
|
||
rb_define_alias(rb_cARGF, "inspect", "to_s");
|
||
rb_define_method(rb_cARGF, "argv", argf_argv, 0);
|
||
rb_define_method(rb_cARGF, "fileno", argf_fileno, 0);
|
numeric.c | ||
---|---|---|
rb_cFixnum = rb_define_class("Fixnum", rb_cInteger);
|
||
rb_define_method(rb_cFixnum, "to_s", fix_to_s, -1);
|
||
rb_define_alias(rb_cFixnum, "inspect", "to_s");
|
||
rb_define_method(rb_cFixnum, "-@", fix_uminus, 0);
|
||
rb_define_method(rb_cFixnum, "+", fix_plus, 1);
|
||
... | ... | |
rb_define_const(rb_cFloat, "NAN", DBL2NUM(NAN));
|
||
rb_define_method(rb_cFloat, "to_s", flo_to_s, 0);
|
||
rb_define_alias(rb_cFloat, "inspect", "to_s");
|
||
rb_define_method(rb_cFloat, "coerce", flo_coerce, 1);
|
||
rb_define_method(rb_cFloat, "-@", flo_uminus, 0);
|
||
rb_define_method(rb_cFloat, "+", flo_plus, 1);
|
object.c | ||
---|---|---|
* obj.inspect -> string
|
||
*
|
||
* Returns a string containing a human-readable representation of <i>obj</i>.
|
||
* By default, if the <i>obj</i> has instance variables, show the class name
|
||
* and instance variable details which is the list of the name and the result
|
||
* of <i>inspect</i> method for each instance variable.
|
||
* Otherwise uses the <i>to_s</i> method to generate the string.
|
||
* If the <i>to_s</i> method is overridden, uses it.
|
||
* By default, show the class name and the list of the instance variables and
|
||
* their values (by calling #inspect on each of them).
|
||
* User defined classes should override this method to make better
|
||
* representation of <i>obj</i>. When overriding this method, it should
|
||
* return a string whose encoding is compatible with the default external
|
||
... | ... | |
* "baz"
|
||
* end
|
||
* end
|
||
* Baz.new.inspect #=> "baz"
|
||
* Baz.new.inspect #=> "#<Baz:0x0300c868>"
|
||
*/
|
||
static VALUE
|
||
rb_obj_inspect(VALUE obj)
|
||
{
|
||
if (RB_TYPE_P(obj, T_OBJECT) && rb_obj_basic_to_s_p(obj)) {
|
||
int has_ivar = 0;
|
||
VALUE *ptr = ROBJECT_IVPTR(obj);
|
||
long len = ROBJECT_NUMIV(obj);
|
||
long i;
|
||
for (i = 0; i < len; i++) {
|
||
if (ptr[i] != Qundef) {
|
||
has_ivar = 1;
|
||
break;
|
||
}
|
||
}
|
||
if (has_ivar) {
|
||
VALUE str;
|
||
const char *c = rb_obj_classname(obj);
|
||
if (rb_ivar_count(obj) > 0) {
|
||
VALUE str;
|
||
const char *c = rb_obj_classname(obj);
|
||
str = rb_sprintf("-<%s:%p", c, (void*)obj);
|
||
return rb_exec_recursive(inspect_obj, obj, str);
|
||
}
|
||
str = rb_sprintf("-<%s:%p", c, (void*)obj);
|
||
return rb_exec_recursive(inspect_obj, obj, str);
|
||
} else {
|
||
return rb_any_to_s(obj);
|
||
}
|
||
return rb_funcall(obj, rb_intern("to_s"), 0, 0);
|
||
}
|
||
... | ... | |
rb_define_method(rb_cModule, ">=", rb_mod_ge, 1);
|
||
rb_define_method(rb_cModule, "initialize_copy", rb_mod_init_copy, 1); /* in class.c */
|
||
rb_define_method(rb_cModule, "to_s", rb_mod_to_s, 0);
|
||
rb_define_alias(rb_cModule, "inspect", "to_s");
|
||
rb_define_method(rb_cModule, "included_modules", rb_mod_included_modules, 0); /* in class.c */
|
||
rb_define_method(rb_cModule, "include?", rb_mod_include_p, 1); /* in class.c */
|
||
rb_define_method(rb_cModule, "name", rb_mod_name, 0); /* in variable.c */
|
||
... | ... | |
rb_cTrueClass = rb_define_class("TrueClass", rb_cObject);
|
||
rb_define_method(rb_cTrueClass, "to_s", true_to_s, 0);
|
||
rb_define_alias(rb_cTrueClass, "inspect", "to_s");
|
||
rb_define_method(rb_cTrueClass, "&", true_and, 1);
|
||
rb_define_method(rb_cTrueClass, "|", true_or, 1);
|
||
rb_define_method(rb_cTrueClass, "^", true_xor, 1);
|
||
... | ... | |
rb_cFalseClass = rb_define_class("FalseClass", rb_cObject);
|
||
rb_define_method(rb_cFalseClass, "to_s", false_to_s, 0);
|
||
rb_define_alias(rb_cFalseClass, "inspect", "to_s");
|
||
rb_define_method(rb_cFalseClass, "&", false_and, 1);
|
||
rb_define_method(rb_cFalseClass, "|", false_or, 1);
|
||
rb_define_method(rb_cFalseClass, "^", false_xor, 1);
|
proc.c | ||
---|---|---|
rb_define_method(rb_cProc, "eql?", proc_eq, 1);
|
||
rb_define_method(rb_cProc, "hash", proc_hash, 0);
|
||
rb_define_method(rb_cProc, "to_s", proc_to_s, 0);
|
||
rb_define_alias(rb_cProc, "inspect", "to_s");
|
||
rb_define_method(rb_cProc, "lambda?", rb_proc_lambda_p, 0);
|
||
rb_define_method(rb_cProc, "binding", proc_binding, 0);
|
||
rb_define_method(rb_cProc, "curry", proc_curry, -1);
|
test/test_pp.rb | ||
---|---|---|
def a.to_s() "aaa" end
|
||
a.instance_eval { @a = nil }
|
||
result = PP.pp(a, '')
|
||
assert_equal("#{a.inspect}\n", result)
|
||
assert_equal("#{a.to_s}\n", result)
|
||
a = 1.0
|
||
a.instance_eval { @a = nil }
|
||
result = PP.pp(a, '')
|
||
... | ... | |
a = Object.new
|
||
def a.to_s() "aaa" end
|
||
result = PP.pp(a, '')
|
||
assert_equal("#{a.inspect}\n", result)
|
||
assert_equal("#{a.to_s}\n", result)
|
||
assert_equal("aaa\n", result)
|
||
end
|
||
end
|
vm.c | ||
---|---|---|
vm->top_self = rb_obj_alloc(rb_cObject);
|
||
rb_define_singleton_method(rb_vm_top_self(), "to_s", main_to_s, 0);
|
||
rb_define_alias(rb_singleton_class(rb_vm_top_self()), "inspect", "to_s");
|
||
/* initialize mark object array */
|
||
vm->mark_object_ary = rb_ary_tmp_new(1);
|