title: Is there an "&& Equals" operator in Ruby
body: <p>I often find myself doing things like this:</p>
  
  <pre><code>do_something if x &amp;&amp; x == y
  </code></pre>
  
  <p>In other works, do something if <code>x</code> is not nil, and it has a value of <code>y</code>.</p>
  
  <p>It would be nice if I could do something like this instead:</p>
  
  <pre><code>do_something if x &amp;&amp;== y
  </code></pre>
  
  <p>Is there an operator that does this?</p>
  
  <hr>
  
  <p>Responses to comments:</p>
  
  <hr>
  
  <p><code>x == y</code> - The problem with this, is that it only tests existence (not nil) if the value of <code>y</code> is known. If <code>y</code> is itself <code>nil</code> then the check fails. So you could end up doing:</p>
  
  <pre><code>y &amp;&amp; x == y
  </code></pre>
  
  <hr>
  
  <p><code>x ||= y</code> - This would assign the value of <code>y</code> to <code>x</code> if <code>x</code> is nil. That's not what I'm looking for. <code>x &amp;&amp;= y</code> doesn't work either for the same reason - it changes the value of <code>x</code> to <code>y</code> if <code>x</code> exists.</p>
  
  <hr>
  
  <p>An example: in my current scenario I want to check that a user has passed to a controller the token associated with them, but I also want to ensure that the token has been assigned. Something like: </p>
  
  <pre><code>do_something if user.token &amp;&amp; user.token == params[:token]
  </code></pre>