When an if test and the code to be executed are placed on separate lines, the then keyword is optional.
When an if test and the code to be executed are placed on separate lines, the then keyword is optional.
There will no doubt be occasions when you will need to take multiple different actions based on several alternative conditions.
Ruby also has a short-form notation for if..then..else in which a question mark ? replaces the if..then part and a colon : acts as else
Syntax
Test Condition ? if true do this : else do this
A simple test like this has only one of two possible results. Either a bit of code is run or it isn’t, depending on whether the test evaluates to true or not.
Be warned that Ruby’s Boolean operators can sometimes behave in a curious and unpredictable manner. For example:
puts( (not( 1==1 )) ) # This is ok
Ruby has two different syntaxes for testing Boolean (true/false) conditions. In the above example, I’ve used the English-language style operators: and, or and not.
Constants in Ruby begin with a capital letter. Class names are con-stants. You can obtain a list of all defined constants using the con-stants method: Object.constants
Ruby provides a pair of methods, catch and throw, which can be used to break out of a block of code when some condition is met.
There is an alternative form of the case statement which is like a shorthand form of a series of if..then..else statements.