Reputation: 11336
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?
Upvotes: 17
Views: 23233
Reputation: 81691
Have you tried seeing if the expression parser mentioned in an answer to your previous question would allow you to do this?
Upvotes: 0
Reputation: 3102
"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")
Upvotes: 10
Reputation: 64177
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.
Upvotes: 47