I have two integers on a line in STDIN
which I would like to read at the same time.
I tried pattern matching like this:
[a, b] = gets.split.map(&:to_i)
However, this failed:
solution.rb:7: syntax error, unexpected '=', expecting keyword_end
[a, b] = gets.split.map(&:to_i)
How can I read two integers from the same line(preferably but not necessarily at the same time)?
You need to remove brackets on the left side:
a, b = gets.split.map(&:to_i)
It's better to be a safe side by using splat operator
a, b, * = gets.split.map(&:to_i)
for more info about splat operator, I've written a blog on Ruby Splat operator
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With