title: Fastest way to do many, many different gsub operations?
body: <p>I am converting tens of thousands of documents from one markup format (apparently some descendant of RUNOFF for which I have minimal documentation) into something that I can feed to LaTeX.  Part of this process involves searching each document for each string that had special meaning in the old markup format and replacing it with the appropriate string for LaTeX.  There are hundreds of these.  My two thoughts so far are to do each as a separate gsub or to make a gsub with a regex that will match (by way of ORing) many of the symbols and then passing the match off to method with a big case statement that will pass back the appropriate substitution.  On the face of it the second way reduces the number of times the each document must be scanned but the overhead of the ORs within the regex may be more costly.  Does anyone have an idea of which is the better approach or if I have missed a good approach?</p>
  
  <p>Here are examples of the two ways I have described.  They may be imperfect.  Just trying to get my point across.</p>
  
  <p>Method 1:</p>
  
  <pre><code>output.gsub!(/a&lt;-"/, '\\"{a}') # ä
  output.gsub!(/a&lt;-^/, '\\^{a}') # â
  output.gsub!(/a&lt;-~/, '\\~{a}') # ã
  ...etc
  </code></pre>
  
  <p>Method 2:</p>
  
  <pre><code>output.gsub!(/a&lt;-"|a&lt;-^|a&lt;-~|etc/) {|match| convert_symbol(match)}
  
  def convert_symbol(input)
    case match
    when 'a&lt;-"'
      '\\"{a}' # ä
    when "a&lt;-^"
      '\\^{a}' # â
    when "a&lt;-~"
      '\\~{a}' # ã
    when 'etc'
      '\\LaTeX...etc'
    end
  end
  </code></pre>