Ruby Unless: How is it Different from the If Statement

Ruby UnlessRuby, like most other programming languages, utilizes a number of statements. These are the while loop, until, for and unless. All these statements have their own specific applications and utilize a particular syntax. Of the statements just listed, in this article we are going to discuss the unless statement in detail and will try to find out how this statement is different from the similar looking if statement in ruby. The unless statement, in its most basic form, is just the opposite of the if statement. In an unless statement, the code is executed when the condition is false, unlike in if where the code is executed when the condition is true. This is the basic difference between these two.

Here we are going to discuss the syntax and working of the unless statement and the if statement in detail and will try and understand their functioning. Before we begin our study, it would be a good idea to go through some of the basics of the programing in Ruby. This will help you to brush up the basics and in case of beginners, would help in building some kind of a base for further study of the more advanced tools. This course here on the basics of programming in ruby will be the perfect place to start.

Talking about the if and unless statements, these are some of the most basic and highly useful programming tools in ruby and it is a must to be familiar with these for becoming a good ruby programmer. Other than these, there are several other important tools as well which you must be familiar with for writing programs of all kinds and becoming a versatile programmer. To know more about these tools and about programming in ruby in general, you can read this great article about learning programming in Ruby.

Now let us start our discussion about the unless and if statements and we will start by introducing the unless statement.

Introducing Unless

The unless statement in Ruby is basically a yes or no statement. By a yes or no statement we mean that this statement will check whether the condition which has been specified is false or not. It should be noted here very carefully that it will check whether the condition is FALSE or not. If it is false, and the answer is yes, a particular block of code will be executed. But if the condition is true and the answer is no then some other block of code defined under the else clause will be executed.

Syntax and Working of Unless

The syntax which is used for writing an unless statement is,

unless condition [then]
code
[else
else_code]
end

In this syntax, ‘condition’ specifies the condition for the unless statement and ‘code’ specifies the code which will be executed if the condition is found to be false. The ‘else_code’ which has been specified under the else clause will be executed instead if the condition is found to be true.

The working of the unless statement is very simple. The condition is tested for whether it is false or not. If it is found to be false then the code is executed but if it is found to be true then else_code is executed.

Introducing the If Statement

The if statement in Ruby is pretty similar to unless in that is also a yes or no statement. Here the condition is checked for if it is true or not. This is the basic difference between the two. If the condition is found to be true a particular block of code is executed and if it is found to be false, some other code, which is written under else cause will be executed.

Syntax and Working of If

The syntax to be used for the if statement is given by,

if condition [then]
code
[else
else_code]
end

In this syntax, the if statement checks the condition which has been specified by ‘condition’. If this condition is found to be true, then the block of code given by ‘code’ is executed. But if the condition is found to be false then the block of code given by ‘else_code’ is executed.

The working again is pretty simple. The condition is tested for being true and if found to be true, the code is executed. If the condition is found to be false, the else_code is executed.

Here is a great course about programming in Ruby which will teach you about all the different statements and tools and their correct syntax in an exhaustive manner and will enable you to write the basic programs using these programming tools.

Difference Between Unless and If

The difference between unless and if statements in ruby is very easy to understand and in a way is pretty self explanatory. They pretty much mean the same in ruby as they do in English. Both of these are simply the opposite of each other. In an if statement the code is executed when the condition which has been specified is true. The if statement checks the condition and if the condition is found to be true then the specified code is executed.

The exact opposite happens in unless. In an unless statement, the code is executed when the condition specified is false. This means that the unless statement checks the condition and if it is found to be false, then the code which has been specified is executed. This basic difference between these two statements is a huge advantage for the programmers as the code can be simplified by using the appropriate statement at the appropriate place and unwanted complexity in the program can be avoided.

This was all about the unless statement in Ruby. The unless statement was described in proper detail here and how this statement is different from the if statement was also discussed. The unless statement provides great flexibility and helps in writing programs more efficiently and in a more simple and uncomplicated manner. When this statement is used along with the other basic programming tools, the programs which can be created are high in functionality and yet very easy to read and write.

The programming techniques which have been discussed here, the ruby unless statement and the ruby if statement, are not self sufficient and a good understanding of the other basic concepts as well will be required for you to be able to use these tools in the best possible way. Here is a great course about the basic concepts of programming in Ruby which will enable you to write simple programs in Ruby utilizing the concepts which have been discussed here.

If you want to go a step further and want to learn ruby at an advanced level then here is another course which covers the concepts of programming in Ruby right from the basics to all the concepts at an advanced level. This course will help you become a master in programming in Ruby. Happy learning!