Let me preface this with saying that I am new to Ruby.
I was trying to do something like this:
raise NoSuchStrategyError unless ((player1[1].downcase && player2[1].downcase) == ( "p" || "r" || "s"))
However it was not working as intended. It only recognized if the first argument was a “p”. If it was an “r” or an “s” it threw the error. I had to write it out the long way like this for it to work:
raise NoSuchStrategyError unless player1[1].downcase == "p" or player1[1].downcase == "s" or player1[1].downcase == "r"
raise NoSuchStrategyError unless player2[1].downcase == "p" or player2[1].downcase == "s" or player2[1].downcase == "r"
Is there a better way to do this shorthand?
This is because
||returns the first argument to it that is truthy. In this case, since"p"is truthy,("p" || "r" || "s")always returns"p". Knowing this, your first statement can be equivalently rewritten as:As Blender hinted at in his comment about Python, you can do:
or more concisely:
Additionally, be careful when using
and&orin Ruby, they are different than&&&||. You can (and should) read more about the difference.