【发布时间】:2018-04-12 19:56:16
【问题描述】:
我有这个数组:
["[]", "[\"https://somewebsite1.com\"]", "[\"https://somewebsite2.com\"]", "[]", "[]", "[]", "---\n- https://somewebsite3.com\n", "--- []\n", "---\n- https://somewebsite4.com\n", "--- []\n", "--- []\n", "---\n- https://somewebsite.com\n", "---\n- https://somewebsite5.com\n", nil, nil, nil, nil, nil]
我该怎么做:
1- 删除所有空的 nil 元素
2-有一个字符串数组:
["https://somewebsite1.com", "https://somewebsite2.com", "https://somewebsite3.com", "https://somewebsite4.com", "https://somewebsite.com", "https://somewebsite5.com"]
array.select &:present?删除nil
然后array.delete_if {|i| i == '[]'} 删除[]
所以我只剩下:
["[\"https://somewebsite1.com\"]", "[\"https://somewebsite2.com\"]", "---\n- https://somewebsite3.com\n", "--- []\n", "---\n- https://somewebsite4.com\n", "--- []\n", "--- []\n", "---\n- https://somewebsite.com\n", "---\n- https://somewebsite5.com\n"]
【问题讨论】:
-
不,那不是数组数组。那是字符串数组,其中字符串包含看似 yaml 代码的内容
-
YAML 和 JSON 的奇怪组合?还是
[]是有效的 YAML,没有前导---? -
@meagar:是的,it appears to be valid yaml
标签: arrays ruby ruby-on-rails-3