title: How to split a string in Ruby?
body: <p>I have special strings like <code>name1="value1" name2='value2'</code>. Values can contain whitespaces and are delimited by either single quotes or double quotes. Names never contain whitespaces. name/value pairs are separated by whitespaces.</p>
  
  <p>I want to parse them into a list of name-value pairs like this</p>
  
  <pre><code>string.magic_split() =&gt; { "name1"=&gt;"value1", "name2"=&gt;"value2" }
  </code></pre>
  
  <p>If Ruby understood lookaround assertions, I could do this by</p>
  
  <pre><code>string.split(/[\'\"](?=\s)/).each do |element|
      element =~ /(\w+)=[\'\"](.*)[\'\"]/
      hash[$1] = $2
  end
  </code></pre>
  
  <p>but Ruby does not understand lookaround assertions, so I am somewhat stuck.</p>
  
  <p>However, I am sure that there are much more elegant ways to solve this problem anyway, so I turn to you. Do you have a good idea for solving this problem?</p>