I am confused about how the Ruby unless conditional works with the || operator.
So what I've got essentially:
<% unless @instance_variable.method || local_variable.another_method %>
code block
<% end %
At the moment, the first part is evaluating to false and the second part to true. And I do not get an error raised, it does what I want. However, if I just write:
<% unless @instance_variable.method %>
code block
<% end %>
I get an error thrown and I get an error when I write:
<% unless @instance_variable.method && local_variable.another_method %>
code block
<% end %>
So my question. If the first part is evaluating to false, will it cut short and go through the code block, and not looking at the other side? If so, why does leaving the second part out throw an error? And hows does it all work?
Apologies if you need the code, I feel like this is a logic/algebra solution.
Unlessis the exact opposite ofIf. It’s a negatedIf.Unlesswill work when the condition isfalseSo by this or condition
||result isfalsewhen both sides arefalseelse the result is always
truebelow code will not execute the puts statement because the condition gives true
And your
&&is worked because it givesfalsewhen execute below stmt andunlessworked with false :-For more here is a reference https://mixandgo.com/learn/if-vs-unless-in-ruby