title: How to split a sentence into multiple parts in Ruby
body: <p>I want to split a main string and create multiples strings with the words obtained in Ruby.</p>
  
  <pre><code>str = "one two three four five"
  </code></pre>
  
  <p>I want to create all these possibilities in an array of strings:</p>
  
  <pre><code>"one"
  "one two"
  "one two three"
  "one two three four" 
  "one two three four five" 
  </code></pre>
  
  <p>but also:</p>
  
  <pre><code>"two three four five"
  "three four five"
  "four five"
  "five"
  </code></pre>
  
  <p>Ideally I would also obtain the strings inside too, but not required:</p>
  
  <pre><code>"two three four"
  "two three"
  "three four"
  </code></pre>
  
  <p>I tried many things but difficult to have a best way to do that.</p>
  
  <p>For example, I tried to work with each_slice:</p>
  
  <pre><code>words = string.split(" ")
          number_of_words = words.length
          max_number_of_slices = number_of_words
          array_of_strings_to_match = []
          number_of_slices = 1
          while (number_of_slices &lt;= max_number_of_slices)
            array = words.each_slice(number_of_slices).map do |a| a.join ' ' end
            array.each do |w| array_of_strings_to_match &lt;&lt; w end
            number_of_slices = number_of_slices + 1
          end
  </code></pre>
  
  <p>But it's not the good way.</p>
  
  <p>Any idea is welcome. :-)</p>
  
  <p>This question is a little different from <a href="https://stackoverflow.com/questions/4508096">this one</a> because I need to split a sentence of words, not a string by letters (even if it's quite the same).</p>