Ruby - Conditionals unless - case

Greetings of the Day!!!

We have gone over all the basics that you will need in order to work with Conditionals, if, else and else if will do everything that you need.However, there are a couple of convenience conditionals that we can also use that work kind of like a shorthand. We are going to look at four of them. There is unless, case, the ternary operator and or/or-equals. So let's start out by looking at unless. It looks like this, exactly like the if statement, but instead of if it's unless. So unless a Boolean is true, which means the same as if not true, it's the reverse.
So instead of being if not x > 3, it's unless x > 3. So unless it's true, do this block of code. Now unless can sometimes tie you in logical knots trying to work through exactly when it's going to be used. So only use it if it makes a lot of sense. If it feels right and it's convenient to use unless, but don't let it drive you crazy. You can always just switch back to if and then the exclamation point for not and it's the same thing.Now you can use else and elseif still with unless, but there is no such thing as unless if.

That doesn't exist. Unless often works best in really simple cases. When you start getting really complex if is the better way to go. Now we saw how we could have multi- layered conditionals right, if something is true elseif something else is true, elseif something else is true and we could keep going. 

When we start to have a lot of these the thing that ends up being better to use as a shorthand is the case operator. So we basically say let's look at a set of cases when something is true, do this. When something else is true, do that. It's the exact same thing as the if statements. We have just rewritten it as the case operator instead.

Now, this might not seem like it's any shorter than what we had before. It's actually more lines of code to do the same thing, where it really starts to pay dividends though is when there is a real parallel sort of assignment where we can say okay, when x = 1 do this, when x = 2 do that, when x = 3 do that and so on, and it's just right down the line in each of these specific cases. Now because that's when case works best is when we are comparing a test value against a given value. There is also another way to use case, which I think is probably even more often used, which is to put the test value right after case.

So case and then x, let's say. When 1, do a block of code. When 2, do a block of code. So this really is more of a shorthand and really saves you from having to write x == 1, x == 2 each time, we just say x when it's 1, when it's 2 when it's 3 and so on. And we can have the else statement there at the end or we could leave that off if we want. Now in some other programming language, this kind of structure is called switch. So if you've been using that, you will want to switch over to start using case instead, because that's what it's going to be in Ruby.
The next one I want to show you is the Ternary operator and what this is it's a shortcut for simple if and else statement. So if something is true, do one thing, else do something else. Well, in the case when those are really short, especially the blocks of code, it's just one very quick little bit of code that we want to write. Writing those five lines takes up a lot of space and we can do a lot faster if we just do it with the ternary operator, Boolean space question mark. Make sure you have space there and then a question mark, and then whatever little tiny bit of code we want to run.



You don't want to run a whole lot of stuff in here. That's not what this is for. This is only when we are just going to do something real quick and we want a very quick conditional. If that's not true, the else is the colon. So space colon space and then the second bit of code we want to do. So as an example, we can have puts x == 1 and then it will either return 1 or not 1 and that's what we will get output, depending on the Boolean x = 1. Now, this is not highly readable code like a lot of Ruby is. So you really want to be careful about using it and use it only in those cases when we are just going to do something very small.
But in those kind cases, it can be a real lifesaver and save you a lot of typing. The last one that I want us to look at is the or and or/or equals operator. Imagine that we have the simple case where we want to say if y has a value, then set x to it, but if not, go to z, which is a default value, a fallback. So if you have y set it to y. If not, fall back to something else. Well, we can use the Or operator for that and we could use it like this. x = y or z. So if y doesn't exist, then it will be z.



Now we use that in our Booleans remember as an or operator and you will remember that it evaluated the first part to see if it was true or not and if not, then it went to see whether the second part was true or not. Well, that's essentially what this is doing. It's checking that first one. If it doesn't exist and doesn't have a value, then it's not true. So it goes to the next part and it does have a value the value z, so that's what it sets to x. So it's worked exactly like our Booleans do, but we can use it for assignment. So this is a real nice shorthand. It's a lot shorter than writing something like this.

The other one that comes up a lot is we want to say unless x already has a value, set x = y. So if x has a value already, just leave it alone, don't set anything. But if not, we want to set it to y. Y is like a default value. Well, the notation for that is going to be x and then the or operator with the equals after it, the same way we had plus equals, minus equals and all those kinds of assignment operators. This is or equals. So if x has a value, we will leave it alone, but if not then we will set x = y, and I think that also is a lot shorter than writing something like this.

So all four of these are really just shorthand methods that allow you to do things a little bit more efficiently. You could use if, else and elseif to accomplish the exact same thing in every case. Go back over it one more time to make sure you understand it. Go into IRB, try all four of them out, get used to how they work, because they really are going to be big timesavers for you.

Hope You Enjoyed!!
Maitrey Patel

Comments