Okay so in Lua
false and
nil will evaluate to
false, any other value will evaluate to
truethe logic behind a normal ternary is
[condition] ? [if true] : [if false] Lua implements the ternary operator differently however, by use of boolean logic
and and
or.
Boolean AND truth table
true and true = true
true and false = false
false and true = false
false and false = false
Boolean OR truth table
true or true = true
true or false = true
false or true = true
false or false = false
Boolean NOT truth table
not true = false
not false = true
now Lua makes use of Short-Circuit Evaluation (I believe they call it Short-Circuit Logic) where when resolving a conditional if the operators used will no longer effect the output, it will stop evaluating and skip the remainder of the conditional. So that is to say, lets assume we have the following conditional
if not underAttack or (health >= 0 and canMove) then
print("The ship is fine")
end
now lets assume
underAttack is `false`,
not false is `true`, now the next thing it would evaluate makes use of an OR taking a look back at the truth table for OR you'll notice that combining anything with `true` will result in `true`, therefore Lua will stop evaluating the conditional and perform the contents of the if statement. Now you might ask, but that is an if statement, what does this have to do with the ternary? well the concept is the same, take the following example (using booleans here for ease of reading)
secondPoll == "A" and true or false
now lets assume
secondPoll is set to
"A", therefore
secondPoll == "A" resolves to `true`, the ternary now reads
true and true or false
now looking at our truth tables, we perform the AND as there are no brackets dictating order, that makes the ternary
true or false
looking at the OR truth table we now know this will resolve to `true`. now lets invert so that false is our 'if-true' value
secondPoll == "A" and false or true
again lets assume
secondPoll is set to A, the ternary now reads
true and false or true
now in AND the truth table anything with `false` is `false`, making it read
false or true
and in the OR true table, anything with `true` is `true` meaning this will also resolve to true! So now that the easier to envision example is over, now lets use something with 'real' variables.
secondPoll == "A" and "B" or "C"
now again assuming
secondPoll is set to A that will resolve to `true`, making it read
true and "B" or "C"
now lets evaluate the AND… `true` AND "B" (don't forget as I said Lua evaluates any non-nil value as `true`) meaning now we have
"B" or "C"
using the OR truth table you'll see that anything and `true` (remember "B" is `true`) is `true` so the conditional stops there and the result is
"B"I hope you followed along fine in this example, however if there's any parts you wish me to explain again, I can do so.
now as for your question about
var = var == true
this is simply just an equality check, equality checks return true or false, therefore in a plain English sentence this says "make var equal whether var is already equal to true" meaning if
var is `true` it will remain `true`, if it is anything else it will become `false`.
this is why doing this
if turtle.dig() == true then
end
is pointless, dig returns true, there is no need to then have the following conditional
if true == true then
end
as `true` will always be equal to `true`, meaning it is a redundant check.
equally as pointless is the following function
local function dig()
if turtle.dig() then
return true
else
return false
end
end
it could simply be
local function dig()
return turtle.dig()
end
I hope all of this makes sense and you've learnt some things about, well, programming in general (truth tables perform the same way in all languages!)
EDIT: in the sense of the question you edited in, I hope you're able to work out the answer from my above explanation that the answer is yes, they are the same, just in the sense of using the booleans like you did in the example they're not.
EDIT 2: oh i should also mention. lets assume we have a variable, and when its nil give it a default value, we can do it like this
var = var or "hello"
so obviously if
var is `false` or `nil` in this case, it will then become
"hello" however if it is anything other value it will remain as the value. which you should also understand from learning the explanations above.