-3

Lets say I have the string "first=53 second=65 third=82". How do I assign each value to variables x, y, z?

Edit: The words must match as well. With the + signs and exact number of spaces.

rightDrop
  • 61
  • 1
  • 11

1 Answers1

6
str = "first=53 second=65 third=82"
x, y, z = str.scan(/\d+/).map(&:to_i)
SHS
  • 7,651
  • 3
  • 18
  • 28
  • Thank you. However, not what I'm looking for. The words must match as well. With the + signs and exact number of spaces. I forgot to mention it in the question. – rightDrop Feb 19 '15 at 20:04
  • Can't think of any intelligent use for that. Anyway, if you already know where everything is in the string, just get it off the string index. `x, y, z = [str[6..7], str[16..17], str[25..26]].map(&:to_i)` – SHS Feb 19 '15 at 20:11