Precedence3:25 with Jason Seifer
Precedence refers to the order in which statements are evaluated in a program. The different boolean values, true, false, and nil, have different precedence in a program. The same is true with different operators like the and and or operators. These can all be combined to make our programs behave differently. It’s important to understand precedence when writing programs in Ruby and precedence can be a little bit nuanced. Let’s take look at boolean precedence now using Workspaces.
true || false
This returns true because it's an either or scenario.
true && false
This returns false because both sides of an
&& statement need to be true for it to pass.
false && true
This statement returns false for the same reason -- both sides must be true.
1 && 2
This returns 2 because it is evaluated from right to left, which evaluates to not false.
1 && 2 == 1 && 2
This returns false because == has higher precedence than
&&. It compiles to the following:
1 && (2 == 1) && 2
Which is really:
1 && false && 2
In this case, both of those evaluate to false.
In order to make that statement evaluate as we expect, it would need to be written in the following way:
1 && 2 == (1 && 2)
Precedence refers to the order in which statements are evaluated in a program.
The different Boolean values, true, false, and
nil, have different precedence in a program.
The same is true with different operators, like the and and or operators.
These can all be combined to make our programs behave differently.
It's important to understand precedence when writing programs in Ruby, and
precedence can be a little bit nuanced.
Let's take a look at Boolean precedence now using workspaces.
So let's go ahead and take a little bit of a look at precedents inside of irb.
So go ahead and type irb to get into interactive Ruby.
So let's take a look at true and false.
If we use the or operator with two pipes.
This is going to return true.
It's going to do that because it's an either, or scenario.
If either side of this has an operand that is technically true then,
that is what will be returned.
However, if we do true && false, this is going to return false,
because each side must evaluate to some sort of true statement.
The same thing is true if we reverse false && true.
Now when I say it has to be sort of true.
The same thing works when you consider nil, nil is falsy.
So if we do something like this, 1 && 2, 2 is not nil.
Neither is 1, but it's going to be evaluated from right to left.
Now this is where it can get a little bit tricky.
If we wanted to see whether or not 1 && 2, which we can see from two lines up,
should return 2 was equal to 1 && 2.
That returns false, the reason is because of the order of evaluation, or precedence.
We know that each 1 and 2 statement should return 2, like it does up here.
So, what's going on?
What's happening is the order in which it's evaluated.
We could think of it as being written like this.
When we put parentheses around a statement,
the result of that expression can be manipulated and used.
So what happens here Is checking whether 2 is equal to 1 returns false.
And just like up here, when we were doing true and false, that returned false.
So this is the same as saying 1 && false && 2.
Since both of these statements, false && 2 would return false,
as well as 1 && false, we get false back.
If we wanted to make this statement equivalent,
we can do (1 && 2) == (1 && 2) in parenthesis.
And that returns true.
You need to sign up for Treehouse in order to download course files.Sign up