如何将带括号的Ruby字符串转换为数组?
发布时间:2020-12-16 23:34:30 所属栏目:百科 来源:网络整理
导读:我想将以下字符串转换为数组/嵌套数组: str = "[[this,is],[a,nested],[array]]"newarray = # this is what I need help with!newarray.inspect # = [['this','is'],['a','nested'],['array']] 解决方法 你会得到你想要的YAML. 但是你的字符串有点问题. YAM
我想将以下字符串转换为数组/嵌套数组:
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"]] (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |