On Fri, Jun 13, 2003 at 03:50:31PM +0900, you CAN teach an old dog ... wrote: > "Hal E. Fulton" <hal9000 / hypermetrics.com> wrote > I guess you know > about p and puts, right? > > > puts obj > > will call obj.to_s, but > > p obj > > will call obj.inspect instead. > > Sounds reasonable, but it does not explain the behavior below. It > seems (p x) calls x.inspect if it is defined; otherwise it calls > x.to_s That's because static VALUE rb_obj_inspect(obj) VALUE obj; { if (TYPE(obj) == T_OBJECT && ROBJECT(obj)->iv_tbl && ROBJECT(obj)->iv_tbl->num_entries > 0) { .... .... } return rb_funcall(obj, rb_intern("to_s"), 0, 0); } So in fact #inspect returns #to_s() when the object has got no instance vars: >> class A; def to_s; "A#to_s" end end => nil >> a = A.new => A#to_s >> a.instance_eval { @a = 1 } => 1 >> a => #<A:0x401d0f20 @a=1> -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com LILO, you've got me on my knees! -- David Black, dblack / pilot.njin.net, with apologies to Derek and the Dominos, and Werner Almsberger