pootzko pootzko - 5 months ago 11
Ruby Question

Splitting string to integer from single-line user input?

I just started learning some ruby, and I want to do something like this:

print "Insert two numbers: "
a, b = gets.split(" ")

but I want to make a and b to be integers at the same time (in the same line).. If I add
to the second line (before or after
split(" ")
), it doesn't work... so, how should I approach this? mapping, splitting, slicing? ok, I know I could use scanf, but other than scanf, how would I do this?

sorry for such a noobish question, but I just couldn't find a good enough answer only googling...

a, b = gets.split(" ").map(&:to_i)

Or more universal:

a, b = gets.scan(/\-?\d+/).map(&:to_i)