Reading several data entries per line from screen

A

Alex DeCaria

Is there a single, easy method in Ruby that will let me read multiple
numbers from the screen directly into multiple variables?

For instance, say I have two variable called "age" and "weight", and
query the user to enter them on the same line. In FORTRAN I'd just say

READ(*,*) age, weight

and when the user enters "20, 200" they are put to age and weight, as
real variables. Can I do a similar thing in Ruby?

I did create a method that will parse the string read by "gets" into
elements of an array, but there must be a simpler way to do this that I
am missing.
 
H

Harry

Is there a single, easy method in Ruby that will let me read multiple
numbers from the screen directly into multiple variables?

For instance, say I have two variable called "age" and "weight", and
query the user to enter them on the same line. In FORTRAN I'd just say

READ(*,*) age, weight

Is this what you are looking for?

name, age, weight = gets.split(/,/)
puts name
puts age
puts weight



Harry
 

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,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top