在Ruby中删除数组开头和结尾的双引号



我有一个存储在文件中的数组,当我在Ruby中将该文件读取为变量时,它在数组的开始和结束处返回双引号。我使用Ruby 2.7.0

irb
irb(main):065:0> ports = IO.read('/ports_values')
irb(main):066:0> ports
=> "['127.0.0.1:6601:6601', '127.0.0.1:8000:8000', '127.0.0.1:7200:7200', '127.0.0.1:9201:9201', '5606:5606', '6304:6504', '6305:6505']"

但是我需要的是:

irb(main):066:0> ports
=> ['127.0.0.1:6601:6601', '127.0.0.1:8000:8000', '127.0.0.1:7200:7200', '127.0.0.1:9201:9201', '5606:5606', '6304:6504', '6305:6505']

没有双引号,我试过了,但是没有工作

irb(main):067:0> ports.gsub /"/, ' '
=> "['127.0.0.1:6601:6601', '127.0.0.1:8000:8000', '127.0.0.1:7200:7200', '127.0.0.1:9201:9201', '5606:5606', '6304:6504', '6305:6505']"
a = "['127.0.0.1:6601:6601', '127.0.0.1:8000:8000', '127.0.0.1:7200:7200', '127.0.0.1:9201:9201', '5606:5606', '6304:6504', '6305:6505']"
a.slice(1..-2).split(",").map{ |b| b.strip.gsub("'","")}

0/p:["127.0.0.1:6601:6601", "127.0.0.1:8000:8000", "127.0.0.1:7200:7200", "127.0.0.1:9201:9201", "5606:5606", "6304:6504", "6305:6505"]

相关内容

  • 没有找到相关文章

最新更新