title: What's the difference between scan and match on Ruby string
body: <p>I am new to Ruby and has always used <code>String.scan</code> to search for the first occurrence of a number. It is kind of strange that the returned value is in nested array, but I just go <code>[0][0]</code> for the values I want. (I am sure it has its purpose, just that I haven't used it yet.)</p>
  
  <p>I just found out that there is a <code>String.match</code> method. And it seems to be more convenient because the returned array is not nested.</p>
  
  <p>Here is an example of the two, first is scan:</p>
  
  <pre><code>&gt;&gt; 'a 1-night stay'.scan(/(a )?(\d*)[- ]night/i).to_a
  =&gt; [["a ", "1"]]
  </code></pre>
  
  <p>then is match</p>
  
  <pre><code>&gt;&gt; 'a 1-night stay'.match(/(a )?(\d*)[- ]night/i).to_a
  =&gt; ["a 1-night", "a ", "1"]
  </code></pre>
  
  <p>I have check the API, but I can't really differentiate the difference, as both referred to 'match the pattern'.</p>
  
  <p>This question is, for simply out curiousity, about what <code>scan</code> can do that <code>match</code> can't, and vise versa. Any specific scenario that only one can accomplish? Is <code>match</code> the inferior of <code>scan</code>?</p>