我有一个像['12','34','35','231']这样的红宝石数组。
我想把它转换成字符串,比如'12' '34' '35' '231'
我该怎么做呢?
我有一个像['12','34','35','231']这样的红宝石数组。
我想把它转换成字符串,比如'12' '34' '35' '231'
我该怎么做呢?
当前回答
我觉得这种方式很有可读性,也很幼稚:
add_quotes =- > x{"'#{x}'"}
p ['12','34','35','231'].map(&add_quotes).join(',') => "'12','34','35','231'"
其他回答
string_arr.map(&:inspect).join(',') # or other separator
> a = ['12','34','35','231']
> a.map { |i| "'" + i.to_s + "'" }.join(",")
=> "'12','34','35','231'"
irb(main):027:0> puts ['12','34','35','231'].inspect.to_s[1..-2].gsub('"', "'")
'12', '34', '35', '231'
=> nil
还有另一种变化
a = ['12','34','35','231']
a.to_s.gsub(/\"/, '\'').gsub(/[\[\]]/, '')
irb(main)> varA
=> {0=>["12", "34", "35", "231"]}
irb(main)> varA = Hash[*ex.collect{|a,b| [a,b.join(",")]}.flatten]
...