|
1 |
| -# About |
| 1 | +# Conditionals |
2 | 2 |
|
3 |
| -An `if` statement can be used to conditionally execute code: |
| 3 | +Ruby has what is known as flow control expressions, these are used to control the way the program will run and they take a truthy or falsey value. |
| 4 | +There are operators that can be used to create truthy or falsey values, these are known as [comparison operators][comparison-operators]. |
| 5 | + |
| 6 | +There are two main control expressions that are used to control which code will run and which will not. |
| 7 | +Also known as which given branch will run. |
| 8 | + |
| 9 | +Those two are: `if` and the `unless` expression. |
| 10 | + |
| 11 | +## Comparison operators |
| 12 | + |
| 13 | +[Comparison operators][comparison-operators] are used to compare values and return a `true` or `false` value. |
| 14 | +The following operators require two values to be compared of the same type. |
| 15 | +If the values are not of the same type then the compiler will throw an error. |
| 16 | +Here is a list of the operators and an example of when they give a `true` value: |
| 17 | + |
| 18 | +| Method | Description | Example | |
| 19 | +| ------ | --------------------- | ------- | |
| 20 | +| < | less than | 5 < 4 | |
| 21 | +| <= | less than or equal | 4 <= 4 | |
| 22 | +| > | greater than | 3 > 1 | |
| 23 | +| >= | greater than or equal | 2 >= 2 | |
| 24 | + |
| 25 | +The equal and not equal operators can be used to compare any type of value contrary to the operators already mentioned. |
| 26 | +The `==` operator is used to check if two values are equal, and that includes checking the type of the value. |
| 27 | +The `!=` works the same way but it will return `true` if the values are not equal and `false` if they are equal. |
| 28 | +Here is a list of the equal and not equal operators and an example of when they give a `true` value: |
| 29 | + |
| 30 | +| Method | Description | Example | |
| 31 | +| ------ | ------------ | ------- | |
| 32 | +| == | equal | 4 == 4 | |
| 33 | +| != | not equal | 5 != 4 | |
| 34 | + |
| 35 | +## Combined comparison operator |
| 36 | + |
| 37 | +The combined comparison operator (sometimes called spaceship operator) is a special comparison operator. |
| 38 | +It is special in the sense that it doesn't return a truthy or falsey value but it returns a number. |
| 39 | +It is written as `<=>` and it is used to compare 2 values. |
| 40 | +It will return `1` if the left value is greater than the right value, `-1` if the left value is less than the right value, and `0` if the values are equal. |
4 | 41 |
|
5 | 42 | ```ruby
|
6 |
| -x = 5 |
| 43 | +1 <=> 2 # => -1 |
| 44 | +2 <=> 2 # => 0 |
| 45 | +3 <=> 2 # => 1 |
| 46 | +``` |
7 | 47 |
|
8 |
| -if x == 5 |
9 |
| - # Execute logic if x equals 5 |
10 |
| -elsif x > 7 |
11 |
| - # Execute logic if x greater than 7 |
12 |
| -else |
13 |
| - # Execute logic in all other cases |
| 48 | +## If statement |
| 49 | + |
| 50 | +The [`if`][if] statement is used to check if a given condition is "truthy" or "falsey". |
| 51 | +If the condition is truthy then the code inside the if statement will run. |
| 52 | +An `if` statement ends with the `end` keyword. |
| 53 | + |
| 54 | +```ruby |
| 55 | +value = 1 |
| 56 | +if value == 1 |
| 57 | + "1 is equal to 1" |
| 58 | +end |
| 59 | +# => "1 is equal to 1" |
| 60 | + |
| 61 | +if value > 2 |
| 62 | + "1 is greater than 2" |
14 | 63 | end
|
| 64 | +# => nil |
15 | 65 | ```
|
16 | 66 |
|
17 |
| -Sometimes you want to execute a statement (or statements) if a condition is _not_ true, for situations like that, Ruby implements the `unless` keyword: |
| 67 | +## Unless statement |
| 68 | + |
| 69 | +The `unless`unless statement works very similarly to the `if` statement but it will run the code inside the `unless` statement if the condition is falsey. |
18 | 70 |
|
19 | 71 | ```ruby
|
20 |
| -x = 4 |
21 |
| -unless x == 5 |
22 |
| - # Execute logic if x does not equal 5 |
| 72 | +value = 1 |
| 73 | +unless value == 1 |
| 74 | + "1 is not equal to 1" |
| 75 | +end |
| 76 | +# => nil |
| 77 | + |
| 78 | +unless value > 2 |
| 79 | + "1 is not greater than 2" |
| 80 | +end |
| 81 | +# => "1 is not greater than 2" |
| 82 | +``` |
| 83 | + |
| 84 | +## Else statement |
| 85 | + |
| 86 | +The `else` statement can be used in conjunction with the `if` and `unless` statements. |
| 87 | +The `else` statement will be executed if the `if` branch or the `unless` branch is not executed. |
| 88 | + |
| 89 | +```ruby |
| 90 | +value = 1 |
| 91 | +if value == 1 |
| 92 | + "1 is equal to 1" |
23 | 93 | else
|
24 |
| - # Execute logic if x == 5 |
| 94 | + "1 is not equal to 1" |
25 | 95 | end
|
| 96 | +# => "1 is equal to 1" |
| 97 | + |
| 98 | +unless value < 2 |
| 99 | + "1 is not greater than 2" |
| 100 | +end |
| 101 | +# => "1 is greater than 2" |
26 | 102 | ```
|
27 | 103 |
|
28 |
| -If you want to execute different code depending on the value of a variable, Ruby's `case` statement might come useful: |
| 104 | +## "Cascading-if" statements |
| 105 | + |
| 106 | +The `elsif` statement can be used in conjunction with the if statement. |
| 107 | +The `elsif` statement will be executed if the if branch is not executed and the condition of the elsif statement is truthy. |
| 108 | +Elsif statements can be chained together and the first truthy condition will be executed. |
| 109 | +There can also be an else statement at the end of the if statement which will run if non of the earlier statement has been true. |
29 | 110 |
|
30 | 111 | ```ruby
|
31 |
| -y = 5 |
32 |
| -case y |
33 |
| -when 3 |
34 |
| - # Execute logic if y equals 3 |
35 |
| -when 5 |
36 |
| - # Execute logic if y equals 5 |
| 112 | +value = 1 |
| 113 | +if value != 1 |
| 114 | + "1 is not equal to 1" |
| 115 | +elsif value > 2 |
| 116 | + "1 is greater than 2" |
37 | 117 | else
|
38 |
| - # Execute logic in all other cases |
| 118 | + "1 is not equal to 1 and 1 is not greater than 2" |
39 | 119 | end
|
| 120 | +# => "1 is not equal to 1 and 1 is not greater than 2" |
40 | 121 | ```
|
41 | 122 |
|
42 |
| -The same problem can sometimes be solved using different types of conditional statements, sometimes one might be more suited for the problem than the other. It's a good idea to stop for a moment and also consider the other two options when using any of the three conditional statements. |
| 123 | +## if and unless as suffix |
| 124 | + |
| 125 | +The if and unless statement can also be used as a [suffix][if-as-suffix], this is useful when you want to run a single line of code if a condition is true. |
| 126 | +It is done by putting the if or unless statement after the code that you want to run. |
| 127 | + |
| 128 | +```ruby |
| 129 | +value = 1 |
| 130 | +"1 is equal to 1" if value == 1 |
| 131 | +# => 1 is equal to 1 |
| 132 | + |
| 133 | +"1 is not equal to 1" unless value == 1 |
| 134 | +# => nil |
| 135 | +``` |
43 | 136 |
|
44 |
| -[arithmetic-operators]: https://www.tutorialspoint.com/ruby/ruby_operators.htm |
45 | 137 | [comparison-operators]: https://www.w3resource.com/ruby/ruby-comparison-operators.php
|
46 |
| -[if-else-unless]: https://www.w3resource.com/ruby/ruby-if-else-unless.php |
47 |
| -[integer-ruby]: https://ruby-doc.org/core-2.7.1/Integer.html |
48 |
| -[float-ruby]: https://ruby-doc.org/core-2.7.1/Float.html |
| 138 | +[if]: https://www.rubyguides.com/ruby-tutorial/ruby-if-else/ |
0 commit comments