# Problem printing variable with $stdout.print

**URL:** <https://www.ruby-forum.com/t/problem-printing-variable-with-stdout-print/120957>\
**Category:** Ruby\
**Created:** [November 22, 2007, 4:58am UTC](https://www.ruby-forum.com/t/problem-printing-variable-with-stdout-print/120957 "2007-11-22T04:58:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [November 22, 2007, 4:58am UTC](https://www.ruby-forum.com/t/problem-printing-variable-with-stdout-print/120957/1 "2007-11-22T04:58:34Z")

</div>

I’m a newbie to Ruby & OO programming, so please bear with me. I  
searched the forum & didn’t find anything relevant. I want to print the  
value of argv[0] to standard output, but I can’t figure out the syntax.  
Below is one of the ways I tried, but it obviously doesn’t work. I’d  
appreciate any advice anyone can provide, along with syntax that will  
get this to work.  
Thanks

```
    $stdout.print "Invalid filename entered: argv[0] \n"
```

---

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [November 22, 2007, 6:38am UTC](https://www.ruby-forum.com/t/problem-printing-variable-with-stdout-print/120957/2 "2007-11-22T06:38:39Z")

</div>

On 22 Nov 2007, at 12:58, Peter V. wrote:

> ```
> $stdout.print "Invalid filename entered: argv[0] \n"
> 
> ```

The literal answer to your question is:

$stdout.print “Invalid filename entered: #{argv[0]}\n”

But that’s probably not what you really want. Firstly, You probably  
mean ARGV instead of argv, so:

$stdout.print “Invalid filename entered: #{ARGV[0]}\n”

But that’s (very, very nearly) the same as just:

print “Invalid filename entered: #{ARGV[0]}\n”

Which in Ruby is better written as:

puts “Invalid filename entered: #{ARGV[0]}”

This is the best place to get started IMHO:

[http://www.rubycentral.com/pickaxe/intro.html](http://www.rubycentral.com/pickaxe/intro.html)

Alex G.

Bioinformatics Center  
Kyoto University

---

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [November 22, 2007, 6:49am UTC](https://www.ruby-forum.com/t/problem-printing-variable-with-stdout-print/120957/3 "2007-11-22T06:49:10Z")

</div>

On Nov 21, 2007, at 10:58 PM, Peter V. wrote:

> ```
> $stdout.print "Invalid filename entered: argv[0] \n"
> 
> ```

puts “Invalid filename entered: #{ARGV[0]}”

Regards, Morton

---

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [November 22, 2007, 8:54am UTC](https://www.ruby-forum.com/t/problem-printing-variable-with-stdout-print/120957/4 "2007-11-22T08:54:52Z")

</div>

On 22 Nov 2007, at 16:36, Peter V. wrote:

> Alex,  
> Thank you very much for your explanation. I have one question about  
> your response. I don’t dispute your statement:
> 
> > Which in Ruby is better written as:
> > 
> > puts “Invalid filename entered: #{ARGV[0]}”  
> > but can you tell me why it’s better to use puts than $stdout.print?  
> > I’m  
> > just curious. Again, thank you very much.  
> > PV

It’s shorter (laziness is a virtue) and more idiomatic, which may or  
may not equal ‘better’ I suppose. According to ‘ri Kernel#puts’ (ruby  
1.8.6) Kernel#puts is equivalent to $stdout.puts so it’s exactly the  
same, just less unnecessary line noise.

Actually in my 2nd Edition Pickaxe it says that Kernel#puts is  
equivalent to STDOUT.puts which is ever so slightly different, but  
there you go.

Alex G.

Bioinformatics Center  
Kyoto University

---

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [November 22, 2007, 4:02pm UTC](https://www.ruby-forum.com/t/problem-printing-variable-with-stdout-print/120957/5 "2007-11-22T16:02:14Z")

</div>

Peter V. wrote:

> but can you tell me why it’s better to use puts than $stdout.print?

On the $stdout vs no $stdout issue:  
Using puts or print is better than using $stdout.puts or $stdout.print  
simply  
because it does the same thing and is less to type.  
On the puts vs. print issue:  
puts isn’t better than print per se - they don’t do the same thing and  
there  
are plenty of cases where you need to use print. It is however better,  
or at  
least more convenient, to use puts if you want a line break instead of  
adding  
the \n yourself. It’s somewhat less to type and more readable.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [November 22, 2007, 8:36am UTC](https://www.ruby-forum.com/t/problem-printing-variable-with-stdout-print/120957/6 "2007-11-22T08:36:09Z")

</div>

Alex,  
Thank you very much for your explanation. I have one question about  
your response. I don’t dispute your statement:

> Which in Ruby is better written as:
> 
> puts “Invalid filename entered: #{ARGV[0]}”  
> but can you tell me why it’s better to use puts than $stdout.print? I’m  
> just curious. Again, thank you very much.  
> PV

Alex G. wrote:

> On 22 Nov 2007, at 12:58, Peter V. wrote:
> 
> > ```
> > $stdout.print "Invalid filename entered: argv[0] \n"
> > 
> > ```
> 
> The literal answer to your question is:
> 
> $stdout.print “Invalid filename entered: #{argv[0]}\n”
> 
> But that’s probably not what you really want. Firstly, You probably  
> mean ARGV instead of argv, so:
> 
> $stdout.print “Invalid filename entered: #{ARGV[0]}\n”
> 
> But that’s (very, very nearly) the same as just:
> 
> print “Invalid filename entered: #{ARGV[0]}\n”
> 
> Which in Ruby is better written as:
> 
> puts “Invalid filename entered: #{ARGV[0]}”
> 
> This is the best place to get started IMHO:
> 
> [http://www.rubycentral.com/pickaxe/intro.html](http://www.rubycentral.com/pickaxe/intro.html)
> 
> Alex G.
> 
> Bioinformatics Center  
> Kyoto University
