Let's tear down the first example.
self.primary_format == "Single" || self.primary_format == "EP"
If you apply operator precedence and add parentheses, you get this:
(self.primary_format == "Single") || (self.primary_format == "EP")
Since #==
is a method, you get this:
(self.primary_format.==("Single")) || (self.primary_format.==("EP"))
Let's fill in a value for self.primary_format
. How about "EP"
.
("EP".==("Single")) || ("EP".==("EP"))
Call the #==
method on both sides and you get
(false) || (true)
Since the left hand side is falsy, we return the right hand side:
true
Now let's tear down the second example.
self.primary_format == "Single" || "EP"
If you apply operator precedence and add parentheses, you get this, because ==
binds more tightly than ||
:
(self.primary_format == "Single") || ("EP")
Once again, let's switch #==
to its method call variant:
(self.primary_format.==("Single")) || "EP"
Let's fill in a "EP"
for self.primary_format
again.
("EP".==("Single")) || "EP")
Call the #==
and you get
(false || "EP")
Since the left hand side of ||
is falsy, the right hand side is returned. So the value is:
"EP"
Which is itself truthy, because it is neither false
nor nil
.
So in summary, you need to think about how an operator like ||
or ==
groups together the expressions on its sides.
The first example says "If this value is equal to 'Single', tell me 'true'. If it's not, if it's equal to 'EP' tell me 'true', otherwise tell me 'false'"
The second example says "If this value is equal to 'Single', tell me 'true'. If it's not, tell me 'EP'".
I hope that helps!