title: Ruby: Using the unless keyword with a return statement
body: <p>I am currently working on a project where I have code that looks like this:</p>
  
  <pre><code>  # the first return is the one causing problems
    def collect
      return Hash["IdentANode", Array[@id, ",", @ident_a_node.collect]] unless @ident_a_node.nil? and @id.nil?
      return Hash["IdentANode", @id] unless @id.nil?
    end
  </code></pre>
  
  <p>Where I use the unless operator to conditionally execute the return statement. For some reason this code <em>still</em> executes even if <code>@ident_a_node</code> is <code>nil</code>. When executing I get this message:</p>
  
  <blockquote>
    <p>IdentANode.rb:14:in <code>collect': undefined method</code>collect' for
    nil:NilClass (NoMethodError)</p>
  </blockquote>
  
  <p>Which confuses me because I had thought that the unless keyword would prevent this from happening. When I change the statement to this form:</p>
  
  <pre><code>if not @ident_a_node.nil? and not @id.nil?
    return Hash["IdentANode", Array[@id, ",", @ident_a_node.collect]]
  end  
  </code></pre>
  
  <p>or this form:</p>
  
  <pre><code>return Hash["IdentANode", Array[@id, ",", @ident_a_node.collect]] if not @ident_a_node.nil? and not @id.nil?
  </code></pre>
  
  <p>The return statement is <em>not</em> executed, what gives? Why is there a difference between these two statements? Does having multiple conditions with the <code>unless</code> keyword cause problems?</p>
  
  <p>Any ideas would be appreciated</p>