Hello dear friends, coders, learners, and enthusiasts!
Today, I am going to draw your attention to conditionals and comparisons in our beloved Ruby PL.
Ruby definitely has the common tools like all other programming languages, but it has even more tricks, as you will see later in this article.
The first thing we have to know is that all comparisons just return "true" or "false". It means that if the comparison returns "true" the expressions within that block will work, otherwise they will not.
As you all know, there are certain operators to accomplish the comparison of items. They all come from maths and are used for various comparison operations. Here they are:
- < (less than)
- > (greater than)
- <= (less than or equal to)
- >= (greater than or equal to)
- == (is equal to)
- != (is not equal to)
The special symbol we have to draw attention to here is the “!” symbol. It has negation effect. Here are a couple of examples:
puts !true # outputs opposite of 'true', so 'false' puts !false # outputs opposite of 'false', so 'true'
Besides those symbols, we can use symbols like “&&” (AND), “||” (OR), to combine comparison operations.
- && (requires all comparisons return true)
- || (requires at least one comparison returns true)
It is enough to use the "if" operator to perform the regular comparison. For example, to check whether the username is "john" or not and output the corresponding message we can write a piece of code like below.
username = "mary" # if comparison (username == "john") returns true # it means we know him and greet him. if username == "john" puts "Hello John !" end
If you paid attention to this, the comparison operation just ends with an "end" keyword. It means the comparison operation and its block ended there.
As you see from the code above, the block inside works only if the comparison returns true. If the result is "false", the block goes on to the latter part, to the piece of code after the block. Let's look at what we can do if the comparison returned "false".
We can accept this operator as an inverse of regular "if". Its block within it works if the comparison returns "false". Here is an example :
username = "baker" # if (username == "john") comparison returns false # then he/she is the guest, not "John". unless username == "john" puts "Hello Guest! Where is John?" end
We could have used this comparison together with the negation operator “!”. For instance:
username = "baker" # if (username == "john") returns false # then he/she is the guest, not "John". if !(username == "john") puts "Hello Guest! Where is John?" end # or if username != "john" puts "Hello Guest! Where is John?" end
As you see, we can use other operators to accomplish what the “unless” operator does. It was just built-in to make it all easier. It depends on your choice which one to use.
We can use more expressions if we have more complicated conditional tools. A set of expressions to perform when the comparison is true and another set when the comparison is false. To accomplish this, besides regular "if" we need to use "else" as well. This "else" means, in other case. Here is an example of this:
# Check whether his age is appropriate to drive a car if age >= 16 puts "You can drive a car..." else puts "Unfortunately, you are not allowed to drive." end
If only one comparison is not enough for you you have to make it more complicated by branching it. You can make more comparisons to accomplish this. You have an even richer set than just "if-else" to do this.
You can add an extra “elsif” operator to do this. It is actually a short version of "else if" words. For instance :
san = -3 if san > 0 puts "Positive number" elsif san < 0 puts "Negative number" else puts "It is equal to 0" end
To make it even more complicated you can use several “elsif” operators. Here is an example:
code = "#Ac30!b9$D" # code - it gets you the first character of 'code'. if code >= 'A' && code <= 'Z' puts "Code starts with an uppercase letter" elsif code >= 'a' && code <= 'z' puts "Code starts with a lowercase letter" elsif code >= '0' && code <= '9' puts "Code starts with a digit" else puts "Code starts with a different symbol." end
Here we also used the “&&” (AND) operator to combine several comparisons if you paid attention to this. We required that the first symbol is equal to or greater than the "A" letter, and at the same time, it is equal to or less than the "Z" letter.
In some cases, we can shorten “if-else” to one line with a ternary operator. This operator is comprised of three parts. That is why it is called the ternary operator. They are:
- operators to perform if condition returns true
- operators to perform if condition returns false
To separate different parts of this operator we use “?” and “:” symbols. Here is an example of this:
# General syntax # condition ? part-2 : part-3 # with regular 'if-else' if points >= 50 puts "Congratulations, you passed the exam" else puts "Unfortunately you failed the exam" end # with ternary operator points >= 50 ? puts "Congratulations" : puts "You failed"
You can use the ternary operator when you want to compress simple comparison into one line. As you see from the example above, it compresses all those 5 lines of code into one line.
There is another method to simplify the regular "if" operator. It is to use it after an expression. This way, the expression will be performed only if the comparison returned "true". For instance:
a_number = 7 # In regular case if a_number.even? puts "An even numer" end # Simplified version puts "An even number" if a_number.even?
Dear friends! Here we come to an end of an article about checking conditions and comparisons in our beloved Ruby PL. Now you can write more complex programs using these tools you just learned. In a short while, I will write about loops in Ruby PL. Until we meet in the next article, Goodbye!