Ruby

37
897 reads

March 5, 2013

if-then


When an if test and the code to be executed are placed on separate lines, the then keyword is optional.

35
870 reads

March 5, 2013

More with if else if


More descriptive Example of if else if

40
972 reads

March 5, 2013

Working with if else if


There will no doubt be occasions when you will need to take multiple different actions based on several alternative conditions.

27
900 reads

March 5, 2013

if else alternative


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

35
865 reads

March 5, 2013

if else


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.

29
809 reads

March 5, 2013

Eccentricities of Boolean Operators


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

23
904 reads

March 5, 2013

and or not


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.

31
834 reads

March 5, 2013

Constants


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

28
867 reads

March 5, 2013

Catch and Throw


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.

40
833 reads

March 5, 2013

Alternative Case Syntax


There is an alternative form of the case statement which is like a shorthand form of a series of if..then..else statements.