[前][次][番号順一覧][スレッド一覧]

ruby-changes:13645

From: matz <ko1@a...>
Date: Thu, 22 Oct 2009 05:17:47 +0900 (JST)
Subject: [ruby-changes:13645] Ruby:r25428 (trunk): * object.c (rb_obj_inspect): print instance variables only when

matz	2009-10-22 05:17:27 +0900 (Thu, 22 Oct 2009)

  New Revision: 25428

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=25428

  Log:
    * object.c (rb_obj_inspect): print instance variables only when
      Object#to_s is not overridden.  [ruby-core:24425]
    
    * class.c (rb_obj_basic_to_s_p): new function.

  Modified files:
    trunk/ChangeLog
    trunk/class.c
    trunk/object.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 25427)
+++ ChangeLog	(revision 25428)
@@ -1,3 +1,10 @@
+Thu Oct 22 04:54:41 2009  Yukihiro Matsumoto  <matz@r...>
+
+	* object.c (rb_obj_inspect): print instance variables only when
+	  Object#to_s is not overridden.  [ruby-core:24425]
+
+	* class.c (rb_obj_basic_to_s_p): new function.
+
 Wed Oct 21 19:32:52 2009  Nobuyoshi Nakada  <nobu@r...>
 
 	* object.c (rb_obj_inspect): fixed rdoc about the case that to_s
Index: object.c
===================================================================
--- object.c	(revision 25427)
+++ object.c	(revision 25428)
@@ -369,16 +369,19 @@
  *  Returns a string containing a human-readable representation of
  *  <i>obj</i>. If not overridden and no instance variables, uses the
  *  <code>to_s</code> method to generate the string.
+ *  <i>obj</i>.  If not overridden, uses the <code>to_s</code> method to
+ *  generate the string.
  *
  *     [ 1, 2, 3..4, 'five' ].inspect   #=> "[1, 2, 3..4, \"five\"]"
  *     Time.new.inspect                 #=> "2008-03-08 19:43:39 +0900"
  */
 
+extern int rb_obj_basic_to_s_p(VALUE);
+
 static VALUE
 rb_obj_inspect(VALUE obj)
 {
-
-    if (TYPE(obj) == T_OBJECT) {
+    if (TYPE(obj) == T_OBJECT && rb_obj_basic_to_s_p(obj)) {
         int has_ivar = 0;
         VALUE *ptr = ROBJECT_IVPTR(obj);
         long len = ROBJECT_NUMIV(obj);
@@ -398,6 +401,7 @@
             str = rb_sprintf("-<%s:%p", c, (void*)obj);
             return rb_exec_recursive(inspect_obj, obj, str);
         }
+	return rb_any_to_s(obj);
     }
     return rb_funcall(obj, rb_intern("to_s"), 0, 0);
 }
Index: class.c
===================================================================
--- class.c	(revision 25427)
+++ class.c	(revision 25428)
@@ -1257,6 +1257,16 @@
     rb_attr(klass, rb_intern(name), read, write, FALSE);
 }
 
+int
+rb_obj_basic_to_s_p(obj)
+{
+    const rb_method_entry_t *me = rb_method_entry(CLASS_OF(obj), rb_intern("to_s"));
+    if (me && me->def && me->def->type == VM_METHOD_TYPE_CFUNC &&
+	me->def->body.cfunc.func == rb_any_to_s)
+	return 1;
+    return 0;
+}
+
 #include <stdarg.h>
 
 int

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]