Search code examples
rubysyntaxrmagick

How do I split an array into a bunch of strings in Ruby?


This method accepts a list of URLs:

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

I have this string "url1, url2, url3".

How can I feed the above method my list as separate arguments? I tried:

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)

Solution

  • * can expand the array to arguments:

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