我想将以下字符串转换为数组/嵌套数组:
str = "[[this, is],[a, nested],[array]]"
newarray = # this is what I need help with!
newarray.inspect # => [['this','is'],['a','nested'],['array']]
Run Code Online (Sandbox Code Playgroud)
Wie*_*czo 11
你会得到你想要的YAML.
但是你的字符串有点问题.YAML希望逗号背后有空格.所以我们需要这个
str = "[[this, is], [a, nested], [array]]"
Run Code Online (Sandbox Code Playgroud)
码:
require 'yaml'
str = "[[this, is],[a, nested],[array]]"
### transform your string in a valid YAML-String
str.gsub!(/(\,)(\S)/, "\\1 \\2")
YAML::load(str)
# => [["this", "is"], ["a", "nested"], ["array"]]
Run Code Online (Sandbox Code Playgroud)