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