What is p's real behavior?

Y

Yu-Hsuan Lai

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

But my $\ is surely nil, it still append a newline after output.
("output record separator" is $\, right?)
Why does it do this?
--=20
Lai, Yu-Hsuan
 
J

Jesús Gabriel y Galán

I read the RDoc and it says:
For each object, directly writes *obj*.inspect followed by the current
output record separator to the program=91s 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 =3D rb_default_rs =3D 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.
 
R

Ryan Davis

=20
Looking into Ruby 1.8.7's source code I see this in io.c:
=20
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);
}
=20
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:
=20
rb_rs =3D rb_default_rs =3D rb_str_new2("\n");
=20
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.
 
R

Ryan Davis

=20
On Mar 17, 2011, at 10:24 , Jes=FAs Gabriel y Gal=E1n wrote:
=20
=20
Couple lines down:
=20
rb_define_hooked_variable("$\\", &rb_output_rs, 0, rb_str_setter);
=20
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,135
Messages
2,570,786
Members
47,342
Latest member
KelseyK737

Latest Threads

Top