Combining Boolean Operators


Alright. I am at a complete lost at what I am supposed to do. Anybody mind explaining it some more.

boolean_1 = (3 < 4 || false) && (false || true)

boolean_1 =

boolean_2 = !true && (!true || 100 != 5**2)

boolean_2 =

boolean_3 = true || !(true || false)

boolean_3 =


Hey @ladylinkstart

Okay, let's try breaking these down one by one.
I'll try to show how the comparisons are broken down into simple true and false statements, so that hopefully the final value will make sense.

boolean_1 = (3 < 4 || false) && (false || true)
First, we essentially have two groupings as defined by the parentheses, (3 < 4 || false) and (false || true). Since they are separated by &&, (called logical AND), it means that both sides needs to be true, in order for it to return true.

So, checking out (3 < 4 || false), this one is split by ||,(called logical OR), which means that either of the sides need to be true, for this one to return true.
Since 3 is less than 4 (3<4), this part will return true to the left side of the '&&' comparison.
boolean_1 = (true) && (false || true)

On the right side, we have (false || true), which will also return true since the right hand side of the || operator is true.
boolean_1 = (true) && (true)

Since we now only have two 'true' statements on each side of the && operator, this one will return true.
boolean_1 = true

Now, onto the next one
boolean_2 = !true && (!true || 100 != 52)**

We'll do the same thing, and start with the inner parentheses.
On the left side of the || operator, we have !true, where the '!' sign in front of true means to get the opposite value, which will be false.
boolean_2 = !true && (false || 100 != 5**2)

On the right side of || operator we have a new operator here, !=, which checks that the values on both sides are NOT equal.
Since 5**2 (** meaning multiply left side number with itself as many times on the right side) is equal to 25, 100 will not be equal to 25 and return true.
boolean_2 = !true && (false || true)

Then to finish up the inner parentheses, we get
boolean_2 = !true && (true)

Since the left side of && operator is !true, it will be false
boolean_2 = false && true

And since && needs both sides to be true, this will fail
boolean_2 = false

And finally the third one
boolean_3 = true || !(true || false)

Run inner parentheses first
boolean_3 = true || !(true)

Now, since there's a ! in front of the parentheses, when removing them, we need to invert the value inside of it
boolean_3 = true || false

And since || only needs true to be on either side
boolean_3 = true

Take notice to the second and third tests.

On the second one, we can see that we need to compare !true with everything else on the right side of &&. However, since !true will be false, the && comparison will already fail, making the rest of the right hand side of the && operator unnecessary in order to find the result.

This is also similar to the third test, where the left hand side of || is true, and with || only needing either side to be true, this one will already be valid without having to check the rest on the right hand side of ||.


Hi! I have this problem "undefined method `<' for false:FalseClass" when i submit the code. And i can't find where is the error. :_ I'm really stuck here :sweat:

true# boolean_1 = (3 < 4 || false) && (false || true)
boolean_1 = (3<4 || 4<3) && (4>9 || 9<45)

false# boolean_2 = !true && (!true || 100 != 5**2)
boolean_2 = !5<9 && (!1<6 || 100 != 5**2)

true# boolean_3 = true || !(true || false)
boolean_3 = 1+2 == 3 || !(1+2 < 2+2 || 2+1 == 2+2)


Hey @mgirola

The error you're getting is occurring on the line "boolean_2 = !5<9 && (!1<6 || 100 != 5**2)", specifically "!5<9"

What's happening here, is that since "!" has a higher operator precedence than "<", meaning that Ruby will resolve "!5" first, turning the result to false (since 5 is a number, it will be interpreted as "True" before being inverted by "!").
At this point, the statement would be read as "false < 9", which gives the error "undefined method `<' for false:FalseClass", since false doesn't have any method named "<".

Now, the way to fix this, would be to encapsulate the "5<9" inside a parentheses, which would make Ruby handle that operation first, before negating the result.

There is also another similar issue in the same line, "!1<6", which would also require to be fixed in order to prevent the same issue.

The final version would look like this : boolean_2 = !(5<9) && (!(1<6) || 100 != 5**2)

You can read a bit more about operator precedense here, if you're interested
Otherwise, let me know if anything's unclear