[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Problem printing variable with $stdout.print

Peter Vanderhaden

11/22/2007 3:59:00 AM

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"
--
Posted via http://www.ruby-....

5 Answers

Alex Gutteridge

11/22/2007 5:38:00 AM

0

On 22 Nov 2007, at 12:58, Peter Vanderhaden wrote:

> 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/...

Alex Gutteridge

Bioinformatics Center
Kyoto University



Morton Goldberg

11/22/2007 5:49:00 AM

0

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

> 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

Peter Vanderhaden

11/22/2007 7:36:00 AM

0

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 Gutteridge wrote:
> On 22 Nov 2007, at 12:58, Peter Vanderhaden 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/...
>
> Alex Gutteridge
>
> Bioinformatics Center
> Kyoto University

--
Posted via http://www.ruby-....

Alex Gutteridge

11/22/2007 7:54:00 AM

0

On 22 Nov 2007, at 16:36, Peter Vanderhaden 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 Gutteridge

Bioinformatics Center
Kyoto University



Sebastian Hungerecker

11/22/2007 3:01:00 PM

0

Peter Vanderhaden 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
--
Jabber: sepp2k@jabber.org
ICQ: 205544826