I do have an array which looks like:
["[0, 1]", "0", "1"]
The question is how to remove brackets inside the array, so the result which I wish to get would be:
["0", "1", "0", "1"]
Currently, I'm out of idea...
This is what I came up with:
["[0, 1]", "0", "1"].to_s.split(/\W/).reject(&:blank?)
If the array elements might be nested, then I would go straight to YAML
or JSON
parsers:
require 'json'
['[0, [1, 0], 1]', '0', '1'].flat_map { |s| JSON.parse(s) }
# => [0, [1, 0], 1, 0, 1]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.