# Beginner Q: Kernel#puts, STDOUT, $stdout relation

**URL:** <https://www.ruby-forum.com/t/beginner-q-kernel-puts-stdout-stdout-relation/78833>\
**Category:** Ruby\
**Created:** [December 8, 2006, 11:18pm UTC](https://www.ruby-forum.com/t/beginner-q-kernel-puts-stdout-stdout-relation/78833 "2006-12-08T23:18:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 8, 2006, 11:18pm UTC](https://www.ruby-forum.com/t/beginner-q-kernel-puts-stdout-stdout-relation/78833/1 "2006-12-08T23:18:20Z")

</div>

Can anybody help me understanding the relation between Kernel#puts,  
STDOUT  
and $stdout?

I thought ‘puts’ is equivalent to ‘$stdout.puts’. Why ‘puts’ doesn’t  
give  
same result as ‘STDOUT.puts’?

class \<\< STDOUT  
def puts(\*args)  
args[0] = "I say " + args[0] unless args.empty?  
super(args)  
end  
end

puts “hello”  
STDOUT.puts “hello”  
$stdout.puts “hello”  
=\> hello  
I say hello  
I say hello

Thank you in advance  
-andre

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 8, 2006, 11:30pm UTC](https://www.ruby-forum.com/t/beginner-q-kernel-puts-stdout-stdout-relation/78833/2 "2006-12-08T23:30:25Z")

</div>

Puts is a method of Kernel.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 8, 2006, 11:44pm UTC](https://www.ruby-forum.com/t/beginner-q-kernel-puts-stdout-stdout-relation/78833/3 "2006-12-08T23:44:26Z")

</div>

> From: Daniel F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> Reply-To: removed\_email\_address@domain.invalid  
> To: removed\_email\_address@domain.invalid (ruby-talk ML)  
> Subject: Re: beginner Q: Kernel#puts, STDOUT, $stdout relation  
> Date: Sat, 9 Dec 2006 07:28:20 +0900
> 
> Puts is a method of Kernel.

But, doesn’t it depend on what IO object $stdout holds? I redefined  
$stdout  
‘puts’ function, but why it does not affect Kernel’s puts method?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 9, 2006, 1:08am UTC](https://www.ruby-forum.com/t/beginner-q-kernel-puts-stdout-stdout-relation/78833/4 "2006-12-09T01:08:32Z")

</div>

On 12/8/06, Andreas S [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > From: Daniel F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > Reply-To: removed\_email\_address@domain.invalid  
> > To: removed\_email\_address@domain.invalid (ruby-talk ML)  
> > Subject: Re: beginner Q: Kernel#puts, STDOUT, $stdout relation  
> > Date: Sat, 9 Dec 2006 07:28:20 +0900
> > 
> > Puts is a method of Kernel.
> 
> But, doesn’t it depend on what IO object $stdout holds? I redefined $stdout  
> ‘puts’ function, but why it does not affect Kernel’s puts method?

It’s a tricky relationship, and I’m not quite sure how or why it works  
this way, but Kernel#puts does not use STDOUT’s (or $stdout, they can  
be different) puts. It does use STDOUT/$stdout (I’m not really sure  
which) but IIRC Kernel#puts is implemented directly using the write  
method. Caveat here though – the write method is called _twice_, once  
for the argument, then again for the newline. So modifying your source  
to rewrite “write” instead of “puts”:

class \<\< STDOUT  
def write(\*args)  
args[0] = "I say " + args[0] unless args.empty?  
super(args)  
end  
end

puts “hello”  
STDOUT.puts “hello”  
$stdout.puts “hello”

produces:

$ ruby test.rb  
I say helloI say  
I say helloI say  
I say helloI say

Jacob F.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 9, 2006, 1:40am UTC](https://www.ruby-forum.com/t/beginner-q-kernel-puts-stdout-stdout-relation/78833/5 "2006-12-09T01:40:37Z")

</div>

On Dec 8, 2006, at 14:16 , Andreas S wrote:

> end  
> end
> 
> puts “hello”

This calls $stdout.write “hello\n”

> STDOUT.puts “hello”  
> $stdout.puts “hello”  
> =\> hello  
> I say hello  
> I say hello

PS:

Please mess with $stdout, not STDOUT, if at all possible.

[http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout](http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout)

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)

I LIT YOUR GEM ON FIRE!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 9, 2006, 1:50am UTC](https://www.ruby-forum.com/t/beginner-q-kernel-puts-stdout-stdout-relation/78833/6 "2006-12-09T01:50:38Z")

</div>

Ah, yes, I decided to snoop around in the c files and this fits what I  
thought I saw in there.

Thanks so much to all.

Regards,

-andre
