From: Ryan Davis Date: 2011-03-18T05:59:07+09:00 Subject: Re: What is p's real behavior? On Mar 17, 2011, at 10:24 , Jesús Gabriel y Galán wrote: > On Thu, Mar 17, 2011 at 6:02 PM, Yu-Hsuan Lai wrote: >> I read the RDoc and it says: >> For each object, directly writes *obj*.inspect followed by the current >> output record separator to the program‘s standard output. >> >> But my $\ is surely nil, it still append a newline after output. >> ("output record separator" is $\, right?) >> Why does it do this? > > Looking into Ruby 1.8.7's source code I see this in io.c: > > void > rb_p(obj) /* for debug print within C code */ > VALUE obj; > { > rb_io_write(rb_stdout, rb_obj_as_string(rb_inspect(obj))); > rb_io_write(rb_stdout, rb_default_rs); > } > > This is the function called by the function defined as 'p'. As you can > see it's outputting the rb_default_rs. Searching for this in the > source, it's only assigned to here, in io.c: > > rb_rs = rb_default_rs = rb_str_new2("\n"); > > So it seems it's assigned to "\n". I don't know if this rb_default_rs > is assigned to something else somewhere else (a grep -r rb_default_rs > * only shows the assignment I showed above), maybe someone with more > knowledge can chime in. If this is not the case, then I guess the > documentation should say "the default record separator". Anyone? Couple lines down: rb_define_hooked_variable("$\\", &rb_output_rs, 0, rb_str_setter); That means that rb_output_rs is hooked up to $\ and changing it in ruby will bridge to C.