Problem printing variable with $stdout.print

P

Peter Vanderhaden

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"
 
A

Alex Gutteridge

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"

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 Gutteridge

Bioinformatics Center
Kyoto University
 
M

Morton Goldberg

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"

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

Regards, Morton
 
P

Peter Vanderhaden

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 said:
$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 Gutteridge

Bioinformatics Center
Kyoto University
 
A

Alex Gutteridge

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 Gutteridge

Bioinformatics Center
Kyoto University
 
S

Sebastian Hungerecker

Peter said:
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
 

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
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top