在rails中将字符串转换为数组数组、字符串、转换为、中将

2023-09-07 10:21:27 作者:谢谢你光临我的梦

我从 rest 客户端发送一个数组,然后像这样接收它:"[1,2,3,4,5]"

I send an array from rest client and received it like this: "[1,2,3,4,5]"

现在我只想将它转换成数组而不使用 Ruby 的 eval 方法.我们可以为此使用任何 Ruby 的默认方法吗?

Now I just want to convert it into Array without using Ruby's eval method. Any Ruby's default method that we could use for this?

 "[1,2,3,4,5]" => [1,2,3,4,5]

推荐答案

也许是这个?

   s.tr('[]', '').split(',').map(&:to_i)