Problem printing variable with $stdout.print

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"

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

Alex G.

Bioinformatics Center
Kyoto University

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

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

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

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

Alex G.

Bioinformatics Center
Kyoto University