如何在Ruby中将数组拆分成一堆字符串?

时间:2013-05-24 14:05:05

标签: ruby syntax rmagick

此方法接受网址列表:

Magick::ImageList.new("img1url", "img2url", "img3url")

我有这个字符串"url1, url2, url3"

如何将上述方法作为单独的参数提供给我的列表?我试过了:

urls = urls.split(',')
Magick::ImageList.new(urls) # fails. It produces an array.

# fails. It produces "'img1url','img2url'".
# The comma is part of the string, but it should split method arguments.
urls = urls.split(',').join("','")
Magick::ImageList.new(urls)

1 个答案:

答案 0 :(得分:4)

*可以将数组扩展为参数:

urls = urls.split(', ')
Magick::ImageList.new(*urls)