p( [ anObject ]+ ) -> nil
For each object, directly writes anObject.inspect followed by the current output record separator to the program's standard output. p bypasses the Ruby I/O libraries.
p self
produces:
main
voidrb_p(VALUEobj)/* for debug print within C code */{VALUEstr=rb_obj_as_string(rb_inspect(obj));// 将对象转换为字符串if(TYPE(rb_stdout)==T_FILE&&rb_method_basic_definition_p(CLASS_OF(rb_stdout),id_write)){io_write(rb_stdout,str,1);io_write(rb_stdout,rb_default_rs,0);}else{rb_io_write(rb_stdout,str);rb_io_write(rb_stdout,rb_default_rs);}}VALUErb_obj_as_string(VALUEobj){VALUEstr;if(TYPE(obj)==T_STRING){// 如果已经是字符串就直接返回returnobj;}str=rb_funcall(obj,id_to_s,0);if(TYPE(str)!=T_STRING)returnrb_any_to_s(obj);if(OBJ_TAINTED(obj))OBJ_TAINT(str);returnstr;}
p Obj 和 p Obj.inspect 区别是前面如果不是 String,就会调用 to_s 后输出,后面已经是 String 了,就直接输出