Given something like this
@grid = "4x3".split("x")
The current result is an array of strings "4","3"
Is there any shortcut to split it directly to integers?
ruby-1.9.2-p136 :001 > left, right = "4x3".split("x").map(&:to_i)
=> [4, 3]
ruby-1.9.2-p136 :002 > left
=> 4
ruby-1.9.2-p136 :003 > right
=> 3
Call map on the resulting array to convert to integers, and assign each value to left and right, respectively.
"4x3".split("x").map(&:to_i)
if you don't wan to be too strict,
"4x3".split("x").map {|i| Integer(i) }
if you want to throw exceptions if the numbers don't look like integers (say, "koi4xfish")
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