summaryrefslogtreecommitdiff
path: root/object.c
diff options
context:
space:
mode:
authoreregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-08-15 11:50:01 +0000
committereregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-08-15 11:50:01 +0000
commitfd7dc23d281f38a71fa7f9c32812cd468c4b1788 (patch)
treef22532584562bfa9d45da35f1c845f415e8dbf4b /object.c
parente272790d1103d12afc98feac8e441fb66147836b (diff)
Kernel#inspect: improve consistency and do not call #to_s.
* object.c (rb_obj_inspect): Kernel#inspect: do not call #to_s. A class can now benefit from the nice default #inspect even if it defines #to_s. Also, there is no more unexpected change in #inspect result. * NEWS: Add note about the change. * bignum.c, io.c, numeric.c, object.c, proc.c, vm.c (Init_*): Adapt internal structures (by aliasing #inspect to #to_s) so they don't rely on the removed behavior (#inspect calling overridden #to_s). * test/ruby/test_object.rb (test_inspect): add tests for Kernel#inspect. * lib/pp.rb (class PP): do not call #to_s anymore, as #inspect no more does (mame). * test/test_pp.rb (class PPInspectTest): remove related assertion (mame). [ruby-core:43238][Feature #6130] * test/drb/drbtest.rb (DRbCore#teardown, DRbAry#teardown): adapt DRb tests with the new change (shirosaki). [ruby-core:47182][Bug #6866] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36709 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'object.c')
-rw-r--r--object.c38
1 files changed, 12 insertions, 26 deletions
diff --git a/object.c b/object.c
index 1e005e53af..b7e1b58c81 100644
--- a/object.c
+++ b/object.c
@@ -450,11 +450,8 @@ inspect_obj(VALUE obj, VALUE str, int recur)
* 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
@@ -479,35 +476,21 @@ inspect_obj(VALUE obj, VALUE str, int recur)
* "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);
}
static VALUE
@@ -2951,6 +2934,7 @@ Init_Object(void)
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 */
@@ -3003,6 +2987,7 @@ Init_Object(void)
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);
@@ -3015,6 +3000,7 @@ Init_Object(void)
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);