title: How to use next with inject in ruby
body: <p>I was writing some code like following:</p>
  
  <pre><code>[1,2,3,4,5].inject([]) do |res, a|
    res &lt;&lt; a*a and next if a == 2
    res &lt;&lt; a
  end
  </code></pre>
  
  <p>It gives following error:</p>
  
  <pre><code>NoMethodError: undefined method `&lt;&lt;' for nil:NilClass
  </code></pre>
  
  <p>As by next it makes <code>res</code> variable as <code>nil</code>, How to work around this problem?</p>
  
  <p>I tried various ways but couldn't get next to work with ruby, I know this snippet I have provided can be done without next(<code>a == 4 ? res &lt;&lt; a*a : res &lt;&lt; a</code>), but in my actual use-case I have some complex logic and can't be done that simply.</p>