0001-object.c-rb_obj_inspect-Kernel-inspect-improve-consi.patch

Benoit Daloze, 04/11/2012 07:56 AM

Download (7.48 KB)

View differences:

bignum.c
3770 3770
    rb_cBignum = rb_define_class("Bignum", rb_cInteger);
3771 3771

  
3772 3772
    rb_define_method(rb_cBignum, "to_s", rb_big_to_s, -1);
3773
    rb_define_alias(rb_cBignum, "inspect", "to_s");
3773 3774
    rb_define_method(rb_cBignum, "coerce", rb_big_coerce, 1);
3774 3775
    rb_define_method(rb_cBignum, "-@", rb_big_uminus, 0);
3775 3776
    rb_define_method(rb_cBignum, "+", rb_big_plus, 1);
io.c
11501 11501
    rb_define_method(rb_cARGF, "initialize", argf_initialize, -2);
11502 11502
    rb_define_method(rb_cARGF, "initialize_copy", argf_initialize_copy, 1);
11503 11503
    rb_define_method(rb_cARGF, "to_s", argf_to_s, 0);
11504
    rb_define_alias(rb_cARGF, "inspect", "to_s");
11504 11505
    rb_define_method(rb_cARGF, "argv", argf_argv, 0);
11505 11506

  
11506 11507
    rb_define_method(rb_cARGF, "fileno", argf_fileno, 0);
numeric.c
3616 3616
    rb_cFixnum = rb_define_class("Fixnum", rb_cInteger);
3617 3617

  
3618 3618
    rb_define_method(rb_cFixnum, "to_s", fix_to_s, -1);
3619
    rb_define_alias(rb_cFixnum, "inspect", "to_s");
3619 3620

  
3620 3621
    rb_define_method(rb_cFixnum, "-@", fix_uminus, 0);
3621 3622
    rb_define_method(rb_cFixnum, "+", fix_plus, 1);
......
3676 3677
    rb_define_const(rb_cFloat, "NAN", DBL2NUM(NAN));
3677 3678

  
3678 3679
    rb_define_method(rb_cFloat, "to_s", flo_to_s, 0);
3680
    rb_define_alias(rb_cFloat, "inspect", "to_s");
3679 3681
    rb_define_method(rb_cFloat, "coerce", flo_coerce, 1);
3680 3682
    rb_define_method(rb_cFloat, "-@", flo_uminus, 0);
3681 3683
    rb_define_method(rb_cFloat, "+", flo_plus, 1);
object.c
449 449
 *     obj.inspect   -> string
450 450
 *
451 451
 * Returns a string containing a human-readable representation of <i>obj</i>.
452
 * By default, if the <i>obj</i> has instance variables, show the class name
453
 * and instance variable details which is the list of the name and the result
454
 * of <i>inspect</i> method for each instance variable.
455
 * Otherwise uses the <i>to_s</i> method to generate the string.
456
 * If the <i>to_s</i> method is overridden, uses it.
452
 * By default, show the class name and the list of the instance variables and
453
 * their values (by calling #inspect on each of them).
457 454
 * User defined classes should override this method to make better
458 455
 * representation of <i>obj</i>.  When overriding this method, it should
459 456
 * return a string whose encoding is compatible with the default external
......
478 475
 *         "baz"
479 476
 *       end
480 477
 *     end
481
 *     Baz.new.inspect                  #=> "baz"
478
 *     Baz.new.inspect                  #=> "#<Baz:0x0300c868>"
482 479
 */
483 480

  
484 481
static VALUE
485 482
rb_obj_inspect(VALUE obj)
486 483
{
487
    if (RB_TYPE_P(obj, T_OBJECT) && rb_obj_basic_to_s_p(obj)) {
488
        int has_ivar = 0;
489
        VALUE *ptr = ROBJECT_IVPTR(obj);
490
        long len = ROBJECT_NUMIV(obj);
491
        long i;
492

  
493
        for (i = 0; i < len; i++) {
494
            if (ptr[i] != Qundef) {
495
                has_ivar = 1;
496
                break;
497
            }
498
        }
499

  
500
        if (has_ivar) {
501
            VALUE str;
502
            const char *c = rb_obj_classname(obj);
484
    if (rb_ivar_count(obj) > 0) {
485
	VALUE str;
486
	const char *c = rb_obj_classname(obj);
503 487

  
504
            str = rb_sprintf("-<%s:%p", c, (void*)obj);
505
            return rb_exec_recursive(inspect_obj, obj, str);
506
        }
488
	str = rb_sprintf("-<%s:%p", c, (void*)obj);
489
	return rb_exec_recursive(inspect_obj, obj, str);
490
    } else {
507 491
	return rb_any_to_s(obj);
508 492
    }
509
    return rb_funcall(obj, rb_intern("to_s"), 0, 0);
510 493
}
511 494

  
512 495

  
......
2917 2900
    rb_define_method(rb_cModule, ">=", rb_mod_ge, 1);
2918 2901
    rb_define_method(rb_cModule, "initialize_copy", rb_mod_init_copy, 1); /* in class.c */
2919 2902
    rb_define_method(rb_cModule, "to_s", rb_mod_to_s, 0);
2903
    rb_define_alias(rb_cModule, "inspect", "to_s");
2920 2904
    rb_define_method(rb_cModule, "included_modules", rb_mod_included_modules, 0); /* in class.c */
2921 2905
    rb_define_method(rb_cModule, "include?", rb_mod_include_p, 1); /* in class.c */
2922 2906
    rb_define_method(rb_cModule, "name", rb_mod_name, 0);  /* in variable.c */
......
2969 2953

  
2970 2954
    rb_cTrueClass = rb_define_class("TrueClass", rb_cObject);
2971 2955
    rb_define_method(rb_cTrueClass, "to_s", true_to_s, 0);
2956
    rb_define_alias(rb_cTrueClass, "inspect", "to_s");
2972 2957
    rb_define_method(rb_cTrueClass, "&", true_and, 1);
2973 2958
    rb_define_method(rb_cTrueClass, "|", true_or, 1);
2974 2959
    rb_define_method(rb_cTrueClass, "^", true_xor, 1);
......
2981 2966

  
2982 2967
    rb_cFalseClass = rb_define_class("FalseClass", rb_cObject);
2983 2968
    rb_define_method(rb_cFalseClass, "to_s", false_to_s, 0);
2969
    rb_define_alias(rb_cFalseClass, "inspect", "to_s");
2984 2970
    rb_define_method(rb_cFalseClass, "&", false_and, 1);
2985 2971
    rb_define_method(rb_cFalseClass, "|", false_or, 1);
2986 2972
    rb_define_method(rb_cFalseClass, "^", false_xor, 1);
proc.c
2190 2190
    rb_define_method(rb_cProc, "eql?", proc_eq, 1);
2191 2191
    rb_define_method(rb_cProc, "hash", proc_hash, 0);
2192 2192
    rb_define_method(rb_cProc, "to_s", proc_to_s, 0);
2193
    rb_define_alias(rb_cProc, "inspect", "to_s");
2193 2194
    rb_define_method(rb_cProc, "lambda?", rb_proc_lambda_p, 0);
2194 2195
    rb_define_method(rb_cProc, "binding", proc_binding, 0);
2195 2196
    rb_define_method(rb_cProc, "curry", proc_curry, -1);
test/test_pp.rb
106 106
    def a.to_s() "aaa" end
107 107
    a.instance_eval { @a = nil }
108 108
    result = PP.pp(a, '')
109
    assert_equal("#{a.inspect}\n", result)
109
    assert_equal("#{a.to_s}\n", result)
110 110
    a = 1.0
111 111
    a.instance_eval { @a = nil }
112 112
    result = PP.pp(a, '')
......
117 117
    a = Object.new
118 118
    def a.to_s() "aaa" end
119 119
    result = PP.pp(a, '')
120
    assert_equal("#{a.inspect}\n", result)
120
    assert_equal("#{a.to_s}\n", result)
121 121
    assert_equal("aaa\n", result)
122 122
  end
123 123
end
vm.c
2246 2246

  
2247 2247
    vm->top_self = rb_obj_alloc(rb_cObject);
2248 2248
    rb_define_singleton_method(rb_vm_top_self(), "to_s", main_to_s, 0);
2249
    rb_define_alias(rb_singleton_class(rb_vm_top_self()), "inspect", "to_s");
2249 2250

  
2250 2251
    /* initialize mark object array */
2251 2252
    vm->mark_object_ary = rb_ary_tmp_new(1);
2252
-