在rails中将字符串转换为Array [英] Convert string into Array in rails
本文介绍了在rails中将字符串转换为Array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从其他客户端发送一个数组,并像这样接收它:"[1,2,3,4,5]"
I send an array from rest client and received it like this: "[1,2,3,4,5]"
现在,我只想将其转换为Array,而无需使用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)
这篇关于在rails中将字符串转换为Array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文