Reading several data entries per line from screen

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.

On 2/3/07, Alex DeCaria [email protected] wrote:

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

Is this what you are looking for?

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

Harry

Yes, Thank You!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs