Ruby provides a special statement which is referred as unless statement. This statement is executed when the given condition is false. It is opposite of if statement. In if statement, the block executes once the given condition is true, however in unless statement, the block of code executes once the given condition is false.
Unless statement is used when we require to print false condition, we cannot use if statement and or operator to print false statements because if statement and or operator always works on true condition.
unless condition # code else # code end
Here else block is executed when the given condition is true.
unless Modifier: You can also use unless as a modifier to modify an expression. When you use unless as a modifier the left-hand side behaves as a then condition and right-hand side behaves as a test condition.
statement unless condition
- Ruby Break and Next Statement
- Ruby redo and retry Statement
- Ruby | Case Statement
- Similarities and Differences between Ruby and C language
- Similarities and Differences between Ruby and C++
- Ruby | Exception Class and its Methods
- Ruby | Dir Class and its methods
- Ruby Date and Time
- Ruby Search and Replace
- BEGIN and END Blocks In Ruby
- Catch and Throw Exception In Ruby
- Ruby getters and setters Method
- Ruby | Class Method and Variables
- Difference Between Laravel and Ruby on Rails
- How to Install Ruby on Rails on Windows and Linux?
- True, False, and Nil In Ruby
- Difference between Perl and Ruby
- And Keyword in Ruby
- Private Classes in Ruby
- Ruby | Matrix cofactor() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.