[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Newbe - strange behavior of print

caof2005

4/11/2007 7:19:00 AM

Hello folks, this is the scenario:
I tried to run the following code in EasyEclipse for Ruby IDE and
SciTE on WinXP
Both has the same behavior.

def main
print "Enter the number of employees: "
numEmp = gets
puts "The number of employees is:= " + numEmp.to_s
end

main

====================

The output is nothing ( it appears that the program hangs-on for a
while ), then if go to the console and I push the ENTER key it appears
the "Enter the number of employees: " and suddenly the program
terminates.

What I'm doing wrong?, I tried to follow the instructions from several
books ( David Black's "Ruby for Rails" and Dave Thomas "Programming
Ruby" ) and both indicates that the kind of code that I wrote should
work.

Any help would be very appreciated.
Regards

8 Answers

Phillip Gawlowski

4/11/2007 7:25:00 AM

0

caof2005 wrote:
> Hello folks, this is the scenario:
> I tried to run the following code in EasyEclipse for Ruby IDE and
> SciTE on WinXP
> Both has the same behavior.
>
> def main
> print "Enter the number of employees: "
> numEmp = gets
> puts "The number of employees is:= " + numEmp.to_s
> end
>
> main
>
> ====================
>
> The output is nothing ( it appears that the program hangs-on for a
> while ), then if go to the console and I push the ENTER key it appears
> the "Enter the number of employees: " and suddenly the program
> terminates.
>
> What I'm doing wrong?, I tried to follow the instructions from several
> books ( David Black's "Ruby for Rails" and Dave Thomas "Programming
> Ruby" ) and both indicates that the kind of code that I wrote should
> work.
>

Have you, per chance, tried to invoke your script from the command line?
I know that SciTE needs to be patched in one way or another to allow for
input. I can't judge Eclipse in that regard, as I don't use it.

--
Phillip "CynicalRyan" Gawlowski
http://cynicalryan....

Rule of Open-Source Programming #9:

Give me refactoring or give me death!

Björn Paetzel

4/11/2007 8:37:00 AM

0

caof2005 schrieb:

Flush your buffers:

> def main
> print "Enter the number of employees: "
$stdout.flush # <- here
> numEmp = gets
> puts "The number of employees is:= " + numEmp.to_s
> end

That should help. :)

caof2005

4/11/2007 9:23:00 AM

0

On Apr 11, 3:36 am, Björn Paetzel <n...@kolrabi.de> wrote:
> caof2005 schrieb:
>
> Flush your buffers:
>
> > def main
> > print "Enter the number of employees: "
>
> $stdout.flush # <- here
>
> > numEmp = gets
> > puts "The number of employees is:= " + numEmp.to_s
> > end
>
> That should help. :)

Bjorn:

Thanks it worked great!
However is it possible you can explain me in general terms why this
behavior happens?
I mean, besides trying in EasyEclipse and SciTE I tried the same code
in irb, and surprisingly it works perfectly (no need to flush)!

Regards



caof2005

4/11/2007 9:26:00 AM

0

On Apr 11, 2:24 am, Phillip Gawlowski <cmdjackr...@googlemail.com>
wrote:
> caof2005 wrote:
> > Hello folks, this is the scenario:
> > I tried to run the following code in EasyEclipse for Ruby IDE and
> > SciTE on WinXP
> > Both has the same behavior.
>
> > def main
> > print "Enter the number of employees: "
> > numEmp = gets
> > puts "The number of employees is:= " + numEmp.to_s
> > end
>
> > main
>
> > ====================
>
> > The output is nothing ( it appears that the program hangs-on for a
> > while ), then if go to the console and I push the ENTER key it appears
> > the "Enter the number of employees: " and suddenly the program
> > terminates.
>
> > What I'm doing wrong?, I tried to follow the instructions from several
> > books ( David Black's "Ruby for Rails" and Dave Thomas "Programming
> > Ruby" ) and both indicates that the kind of code that I wrote should
> > work.
>
> Have you, per chance, tried to invoke your script from the command line?
> I know that SciTE needs to be patched in one way or another to allow for
> input. I can't judge Eclipse in that regard, as I don't use it.
>
> --
> Phillip "CynicalRyan" Gawlowskihttp://cynicalryan....
>
> Rule of Open-Source Programming #9:
>
> Give me refactoring or give me death!

Phillip:

Yes I tried from within irb... and it works perfectly, however when I
use either SciTE or EasyEclipse the behavior is awkward.

Regards
Carlos

Björn Paetzel

4/11/2007 9:48:00 AM

0

caof2005 schrieb:

> Thanks it worked great!
> However is it possible you can explain me in general terms why this
> behavior happens?

The output is being buffered to save system calls which might be costly.
It is actually committed to the operating system when the buffer fills
up, when a newline is sent (print '\n', or puts) or when explicitly told
so (flush).

> I mean, besides trying in EasyEclipse and SciTE I tried the same code
> in irb, and surprisingly it works perfectly (no need to flush)!

Because irb's prompt contains a newline. :)

Marcin Raczkowski

4/11/2007 9:51:00 AM

0

On Wednesday 11 April 2007 09:25, caof2005 wrote:
> On Apr 11, 3:36 am, Björn Paetzel <n...@kolrabi.de> wrote:
> > caof2005 schrieb:
> >
> > Flush your buffers:
> > > def main
> > > print "Enter the number of employees: "
> >
> > $stdout.flush # <- here
> >
> > > numEmp = gets
> > > puts "The number of employees is:= " + numEmp.to_s
> > > end
> >
> > That should help. :)
>
> Bjorn:
>
> Thanks it worked great!
> However is it possible you can explain me in general terms why this
> behavior happens?
> I mean, besides trying in EasyEclipse and SciTE I tried the same code
> in irb, and surprisingly it works perfectly (no need to flush)!
>
> Regards

Irb have Readline support and also automatically flushes everything - just in
case :] by deafault print don't flush - if you want flushing - use puts

Björn Paetzel

4/11/2007 9:54:00 AM

0

Björn Paetzel schrieb:

> Because irb's prompt contains a newline. :)

Ah no.. wait... It seems irb changes to unbuffered output mode, that's why.

Drew Olson

4/11/2007 9:44:00 PM

0

> Phillip:
>
> Yes I tried from within irb... and it works perfectly, however when I
> use either SciTE or EasyEclipse the behavior is awkward.
>
> Regards
> Carlos

Carlos -

I can't speak to SciTE, but Eclipse (specifically with RDT) has an issue
with the syncing of input/output.

See the first question at
http://rubyeclipse.sourceforge.net/fa... for information about
resolving the issue. Or just run from the command line.

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