简体   繁体   English

从Ruby中数组的开始和结束中删除双引号

[英]Removing double quotes from the beging and end of an array in Ruby

I have an array stored in a file, when I read this file to variable in Ruby it returns double quotes at the beginning and end of the array.我有一个存储在文件中的数组,当我将此文件读取到 Ruby 中的变量时,它会在数组的开头和结尾返回双引号。 I use Ruby 2.7.0我使用 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']"

But what I need this:但我需要这个:

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']

without double quotes and I tried this ,but it did not work没有双引号,我试过了,但没有用

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("'","")}

o/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"] o/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"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM