summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog7
-rw-r--r--class.c10
-rw-r--r--object.c8
3 files changed, 23 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index b8a5ddfc2..5f21c0be5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+Thu Oct 22 04:54:41 2009 Yukihiro Matsumoto <matz@ruby-lang.org>
+
+ * 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@ruby-lang.org>
* object.c (rb_obj_inspect): fixed rdoc about the case that to_s
diff --git a/class.c b/class.c
index 865197b84..0f4394d97 100644
--- a/class.c
+++ b/class.c
@@ -1257,6 +1257,16 @@ rb_define_attr(VALUE klass, const char *name, int read, int write)
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
diff --git a/object.c b/object.c
index fc2c23716..6e6934373 100644
--- a/object.c
+++ b/object.c
@@ -369,16 +369,19 @@ inspect_obj(VALUE obj, VALUE str, int recur)
* 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 @@ rb_obj_inspect(VALUE obj)
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);
}