What is p's real behavior?

I read the RDoc and it says:
For each object, directly writes obj.inspect followed by the current
output record separator to the programs standard output.

But my $\ is surely nil, it still append a newline after output.
(“output record separator” is $, right?)
Why does it do this?

On Thu, Mar 17, 2011 at 6:02 PM, Yu-Hsuan L. [email protected]
wrote:

I read the RDoc and it says:
For each object, directly writes obj.inspect followed by the current
output record separator to the programs 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?

Jesus.

puts and p do not honor $, print does.

On Mar 17, 2011, at 10:24 , Jess Gabriel y Galn wrote:

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.

On Mar 17, 2011, at 13:59 , Ryan D. wrote:

Why does it do this?

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.

DOH… Wow. I’m tired. Apparently my espresso hasn’t hit the bloodstream
yet. I overlooked rb_output_rs vs rb_default_rs. Xavier is right, ‘p’
doesn’t honor $\ at all. The doco is wrong. I’ll change it.