摆脱数组中的双引号,而不用将数组转换成字符串 [英] Getting rid of double quotes inside array without turing array into a string

查看:458
本文介绍了摆脱数组中的双引号,而不用将数组转换成字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组


x = [1,2,3,:*, :+,4,5,: - ,:/]

x=["1", "2", "3", ":*", ":+", "4", "5", ":-", ":/"]

摆脱数组内的双引号。

I'm trying to get rid of the double quotation inside of the array.

我知道我可以使用:


x.to_s.gsub(' ','')

x.to_s.gsub('"','')

,它将输出一个字符串:

and it will output a string:


[1,2,3,:*,:+,4,5,...,,/]

"[1, 2, 3, :*, :+, 4, 5, :-, :/]"

我想要的输出是不是字符串的数组:

The desired output I want is an array not a string:


[1,2,3,... * :+,4,5,... - ,:/]

[1, 2, 3, :*, :+, 4, 5, :-, :/]

有没有办法摆脱双引号数组的每个元素,但仍然将数组作为数组?

Is there a way for me to get rid of the double quotes for each element of the array but still leave my array as an array?

提前感谢

推荐答案

eval x.to_s.gsub('"', '')
# => [1, 2, 3, :*, :+, 4, 5, :-, :/]

这篇关于摆脱数组中的双引号,而不用将数组转换成字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆