I am trying to learn ruby. What is an elegant way to convert '"abcd" "efg"' to a ['abcd', 'efg'] in Ruby?
Thanks.
Try this:
require 'shellwords'
'"abcd" "efg"'.shellsplit
#=> ["abcd", "efg"]
You can use scan which finds all occurrences of a pattern within a string.
'"aaa" "bbb"'.scan(/"([^"]*)"/)
=> [["aaa"], ["bbb"]]
Explained:
/ something/ is a regular expression (which can match strings)/" something "/ is a regular expression that matches strings starting and ending in "[^"] matches against any character that isn't a "[^"]* matches as many of these characters as it can.( and ) tell it want the bits I want in the results." characters.