Eric Hodel
5/22/2009 3:58:00 PM
On May 21, 2009, at 21:27, Robert Klemme wrote:
> On 20.05.2009 21:01, Eric Hodel wrote:
>> On May 19, 2009, at 23:14, Robert Klemme wrote:
>>> Btw, in 1.8 there is also $defout - I believe _that- is the stream
>>> used by Kernel#puts and the like in those versions:
>>>
>>> 08:13:00 ~$ ruby -r stringio -e 'puts 1; $defout=StringIO.new;
>>> puts 2'
>>> 1
>>> -e:1: warning: $defout is obsolete; use $stdout instead
>> This warning says otherwise.
>
> Please do not let yourself be distracted by the warning. The
> crucial point is that after $defout has been reassigned the "2" does
> not appear any more on the screen => this is the object which is
> used by #puts.
Other than the warning, the behavior is the same with $stdout and
$defout as they are the same variable.
>> They happen to be the same object though:
>> $ ruby -e 'p $stdout.object_id, $defout.object_id'
>> 97260
>> 97260
>
> Yes - until you reassign any of them. :-)
If you reassign either of them they're still the same object:
$ ruby -r stringio -e '$stdout=StringIO.new; $stderr.puts [$stdout,
$defout].inspect'
[#<StringIO:0x28a50>, #<StringIO:0x28a50>]
From io.c:
rb_define_hooked_variable("$stdout", &rb_stdout, 0, stdout_setter);
[...]
rb_define_hooked_variable("$defout", &rb_stdout, 0, defout_setter);