title: How do I escape parentheses and brackets in Ruby?
body: <p>I am trying to escape parentheses and brackets in a short Ruby script. The script takes the name of the directory as an argument, makes a playlist of the .mp3s in the directory, and names the playlist from the directory. </p>
  
  <p>If the directory has braces or parentheses, it fails without an error, it just doesnt make the playlist. Here is the code:</p>
  
  <pre><code>  1 #!/usr/bin/ruby -w
    2 
    3 # create a playlist for a specified directory of .mp3 files
    4 
    5 puts "enter path to album:"
    6 chomped = gets.chomp
    7 path = File.expand_path("#{chomped}")
    8 
    9 data = "#EXTM3U\n"
   10 tracks = Dir["#{path}/*.mp3"]
   11 name = path.sub(/.*\/(.*)/,"\\1")
   12 name.gsub!(/_/," ")
   13 name.gsub!(/(\A|\s)\w/) { |c| c.upcase }
   14 tracks.sort.each do |track|
   15     track_name = track.sub(/.*\/(.*)/,"\\1")
   16     data += "#EXTINF:\n" + track_name + "\n"
   17 end
   18 File.open("#{path}/#{name}.m3u","a") { |f| f.write data }
   19 puts "created #{path}/#{name}.m3u"
  </code></pre>
  
  <p>I have been able to escape the underscores, and I think I got a regex working to escape the other characters, (<a href="http://rubular.com/r/BNUnjSde6J" rel="nofollow">http://rubular.com/r/BNUnjSde6J</a>) but it still fails using <code>.gsub!</code>. Also, I would rather not escape them, I'd like the title to include them. Would <code>Regexp.escape</code> be an answer?</p>
  
  <p>EDIT: to clarify, the tracknames need not be full paths because the playlist will reside in the same directory as the tracks themselves. see: <a href="http://en.wikipedia.org/wiki/M3U" rel="nofollow">http://en.wikipedia.org/wiki/M3U</a></p>