Please Help I'm new in Ruby -- Input Problem

N

Nolly Mejia

Hi,

I'm interested to learn this language. I'm using Eclipse with Ruby
plugin. I encountered this problem. I place this code (got from an
internet site):

def addtwo(a, b)
a + b; # return a + b
end

print "Please enter number 1 : ";

val1 = gets;
print "Please enter number 2 : ";
val2 = gets;
print "Answer : " , (val1.to_i + val2.to_i), "\n";

It actually works! But the this is when I run this, the console doesn't
show the first print (print "Please enter number 1 : ";) it waits for me
to place a number, then another number before it shows the print. See
below:

1
2
Please enter number 1 : Please enter number 2 : Answer : 3

Please help me.
Thank you!
Nolly
 
M

Marnen Laibow-Koser

Nolly said:
Hi,

I'm interested to learn this language. I'm using Eclipse with Ruby
plugin. I encountered this problem. I place this code (got from an
internet site):

def addtwo(a, b)
a + b; # return a + b
end

print "Please enter number 1 : ";

val1 = gets;
print "Please enter number 2 : ";
val2 = gets;
print "Answer : " , (val1.to_i + val2.to_i), "\n";

Whatever site this code came from can be considered untrustworthy.
Clearly, whoever wrote it doesn't know enough about Ruby to know that
it's considered poor style to put semicolons at the end of lines. And
if they don't know something that basic, what else are they screwing up?
It actually works! But the this is when I run this, the console doesn't
show the first print (print "Please enter number 1 : ";) it waits for me
to place a number, then another number before it shows the print. See
below:

1
2
Please enter number 1 : Please enter number 2 : Answer : 3

Please help me.

Your console output is probably being buffered by line. Try putting a
\n in the string and see if that solves the problem.
Thank you!
Nolly

Best,
-- 
Marnen Laibow-Koser
http://www.marnen.org
(e-mail address removed)
 
N

Nolly Mejia

Your console output is probably being buffered by line. Try putting a
\n in the string and see if that solves the problem.


Best,
-- 
Marnen Laibow-Koser
http://www.marnen.org
(e-mail address removed)

Hi Marnen,

Thank you for helping I really appreciate, but I still have the same
problem.
Here's the output when I placed \n

3
2
Please enter number 1 :
Please enter number 2 :
Answer : 5

I have Final Report on my class, regarding aspiring programming
language. I hope some one can find way.
 
N

Nolly Mejia

Matthew said:
Try replacing print with puts

It does look as though your output is being buffered. Which is odd.
You may need to add explicit STDOUT.flush after your print statements.

Matt


Hi Matt,

GREAT! It works..thanks for helping me.=D
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top