0

I'm trying to do something like:

list = Dir["*.mp4"]
`zip "test.zip" "#{list}"`

But #{list} is coming out as an array, how do I fix that?

0

3 Answers 3

4

You should use Shellwords from the standard library, which is designed to do exactly this—and does proper escaping no matter how weird your filenames are:

require 'shellwords'

list = Dir["*.mp4"]

puts [ "zip", "test.zip", *list ].shelljoin
# => zip test.zip foo.mp4 filename\ with\ spaces.mp4 etc.mp4
Sign up to request clarification or add additional context in comments.

1 Comment

Very nice! Amazing the standard libraries available. As soon as you feel like you know them all someone points out a new one.
2

Doesn't look like you're storing the result anywhere so you should use the multi-argument form of system and bypass the shell entirely:

system('zip', 'test.zip', *list)

Since no shell is invoked, you don't have to worry about quoting or parsing or any of that nonsense, just build a list of strings and splat it.

If you do need to capture the output, then use one of the Open3 methods. Backticks are almost always the wrong approach, there are too many sharp edges (just browse the CERT reports for Ruby and you'll see how often backticks and the single argument form of system cause problems).

Comments

0

http://www.ruby-doc.org/core-2.1.2/Array.html#method-i-join

You are looking for the join method

["a","b","c"].join(" ") => "a b c"

["a","b","c"].join("-|-") => "a-|-b-|-c"

1 Comment

looking at his code i would add a line for ["a","b","c"].map{|x| "'x'"}.join(" ") as it seems he might need the quotations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.