aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog23
-rw-r--r--bignum.c1
-rw-r--r--io.c1
-rw-r--r--lib/pp.rb11
-rw-r--r--numeric.c2
-rw-r--r--object.c38
-rw-r--r--proc.c1
-rw-r--r--test/ruby/test_object.rb25
-rw-r--r--test/test_pp.rb1
-rw-r--r--vm.c1
10 files changed, 37 insertions, 67 deletions
diff --git a/ChangeLog b/ChangeLog
index a940522549..fc8ae2c2a9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,26 +1,3 @@
-Tue Aug 14 19:19:10 2012 Benoit Daloze <eregontp@gmail.com>
-
- update PP with recent Kernel#inspect change. Patch by Yusuke Endoh.
-
- * lib/pp.rb (class PP): do not call #to_s anymore, as #inspect no more does.
- * test/test_pp.rb (class PPInspectTest): remove related assertion.
- [ruby-core:43238][Feature #6130]
-
-Tue Aug 14 19:09:38 2012 Benoit Daloze <eregontp@gmail.com>
-
- Kernel#inspect: improve consistency and 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. Internal structures have been adapted so they don't rely
- on the removed behavior (#inspect calling overridden #to_s).
-
- * object.c (rb_obj_inspect): Kernel#inspect: do not call #to_s.
- * test/ruby/test_object.rb (test_inspect): add tests for Kernel#inspect.
- * bignum.c, io.c, numeric.c, object.c, proc.c, vm.c (Init_*):
- alias #inspect to #to_s where it was expected.
- [ruby-core:43238][Feature #6130]
-
Tue Aug 14 16:25:46 2012 Shugo Maeda <shugo@ruby-lang.org>
* test/erb/test_erb.rb (test_html_escape): add assertions for the
diff --git a/bignum.c b/bignum.c
index fc713a00cd..3394825ce8 100644
--- a/bignum.c
+++ b/bignum.c
@@ -3835,7 +3835,6 @@ Init_Bignum(void)
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);
diff --git a/io.c b/io.c
index b1a38cc707..52eb49574b 100644
--- a/io.c
+++ b/io.c
@@ -11505,7 +11505,6 @@ Init_IO(void)
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);
diff --git a/lib/pp.rb b/lib/pp.rb
index 6e0c797d2e..94269abe65 100644
--- a/lib/pp.rb
+++ b/lib/pp.rb
@@ -265,7 +265,8 @@ class PP < PrettyPrint
module ObjectMixin
# 1. specific pretty_print
# 2. specific inspect
- # 3. generic pretty_print
+ # 3. specific to_s
+ # 4. generic pretty_print
# A default pretty printing method for general objects.
# It calls #pretty_print_instance_variables to list instance variables.
@@ -282,10 +283,18 @@ class PP < PrettyPrint
inspect_method = method_method.call(:inspect)
rescue NameError
end
+ begin
+ to_s_method = method_method.call(:to_s)
+ rescue NameError
+ end
if inspect_method && /\(Kernel\)#/ !~ inspect_method.inspect
q.text self.inspect
elsif !inspect_method && self.respond_to?(:inspect)
q.text self.inspect
+ elsif to_s_method && /\(Kernel\)#/ !~ to_s_method.inspect
+ q.text self.to_s
+ elsif !to_s_method && self.respond_to?(:to_s)
+ q.text self.to_s
else
q.pp_object(self)
end
diff --git a/numeric.c b/numeric.c
index 5ac5449238..73626e1c05 100644
--- a/numeric.c
+++ b/numeric.c
@@ -3660,7 +3660,6 @@ Init_Numeric(void)
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);
@@ -3721,7 +3720,6 @@ Init_Numeric(void)
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);
diff --git a/object.c b/object.c
index b7e1b58c81..1e005e53af 100644
--- a/object.c
+++ b/object.c
@@ -450,8 +450,11 @@ 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, show the class name and the list of the instance variables and
- * their values (by calling #inspect on each of them).
+ * 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.
* 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
@@ -476,21 +479,35 @@ inspect_obj(VALUE obj, VALUE str, int recur)
* "baz"
* end
* end
- * Baz.new.inspect #=> "#<Baz:0x0300c868>"
+ * Baz.new.inspect #=> "baz"
*/
static VALUE
rb_obj_inspect(VALUE obj)
{
- if (rb_ivar_count(obj) > 0) {
- VALUE str;
- const char *c = rb_obj_classname(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);
- str = rb_sprintf("-<%s:%p", c, (void*)obj);
- return rb_exec_recursive(inspect_obj, obj, str);
- } else {
+ 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);
}
static VALUE
@@ -2934,7 +2951,6 @@ 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 */
@@ -2987,7 +3003,6 @@ 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);
@@ -3000,7 +3015,6 @@ 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);
diff --git a/proc.c b/proc.c
index 9ed6dbb2e7..6ccb888bd7 100644
--- a/proc.c
+++ b/proc.c
@@ -2217,7 +2217,6 @@ Init_Proc(void)
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);
diff --git a/test/ruby/test_object.rb b/test/ruby/test_object.rb
index 61482f7c1a..70dcd04201 100644
--- a/test/ruby/test_object.rb
+++ b/test/ruby/test_object.rb
@@ -690,31 +690,6 @@ class TestObject < Test::Unit::TestCase
assert_equal(true, s.tainted?)
end
- def test_inspect
- x = Object.new
- assert_match(/\A#<Object:0x\h+>\z/, x.inspect)
-
- x.instance_variable_set(:@ivar, :value)
- assert_match(/\A#<Object:0x\h+ @ivar=:value>\z/, x.inspect)
-
- x = Object.new
- x.instance_variable_set(:@recur, x)
- assert_match(/\A#<Object:0x\h+ @recur=#<Object:0x\h+ \.\.\.>>\z/, x.inspect)
-
- x = Object.new
- x.instance_variable_set(:@foo, "value")
- x.instance_variable_set(:@bar, 42)
- assert_match(/\A#<Object:0x\h+ (?:@foo="value", @bar=42|@bar=42, @foo="value")>\z/, x.inspect)
-
- # #inspect does not call #to_s anymore
- feature6130 = '[ruby-core:43238]'
- x = Object.new
- def x.to_s
- "to_s"
- end
- assert_match(/\A#<Object:0x\h+>\z/, x.inspect, feature6130)
- end
-
def test_exec_recursive
Thread.current[:__recursive_key__] = nil
a = [[]]
diff --git a/test/test_pp.rb b/test/test_pp.rb
index acd3e835b9..fe65287d88 100644
--- a/test/test_pp.rb
+++ b/test/test_pp.rb
@@ -118,6 +118,7 @@ class PPInspectTest < Test::Unit::TestCase
def a.to_s() "aaa" end
result = PP.pp(a, '')
assert_equal("#{a.inspect}\n", result)
+ assert_equal("aaa\n", result)
end
end
diff --git a/vm.c b/vm.c
index 80dd512ae1..30d0f8a7bd 100644
--- a/vm.c
+++ b/vm.c
@@ -2229,7 +2229,6 @@ Init_top_self(void)
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);