Skip to content

Commit 315e08b

Browse files
authored
[Refactor Concept]: conditionals (#1597)
* Refactor conditionals in Ruby concepts and add new links * Changes based on feedback
1 parent 8a8da22 commit 315e08b

File tree

5 files changed

+319
-71
lines changed

5 files changed

+319
-71
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"blurb": "Ruby has several constructs to conditionally execute code: if statements, unless statements and case statements.",
3-
"authors": ["dvik1950"],
2+
"blurb": "Ruby has conditionals to control the flow of your program. You can use if, unless to control the flow of your program.",
3+
"authors": ["dvik1950", "meatball133"],
44
"contributors": ["kotp", "iHiD"]
55
}

concepts/conditionals/about.md

Lines changed: 117 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,138 @@
1-
# About
1+
# Conditionals
22

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.
441

542
```ruby
6-
x = 5
43+
1 <=> 2 # => -1
44+
2 <=> 2 # => 0
45+
3 <=> 2 # => 1
46+
```
747

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"
1463
end
64+
# => nil
1565
```
1666

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.
1870

1971
```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"
2393
else
24-
# Execute logic if x == 5
94+
"1 is not equal to 1"
2595
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"
26102
```
27103

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.
29110

30111
```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"
37117
else
38-
# Execute logic in all other cases
118+
"1 is not equal to 1 and 1 is not greater than 2"
39119
end
120+
# => "1 is not equal to 1 and 1 is not greater than 2"
40121
```
41122

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+
```
43136

44-
[arithmetic-operators]: https://www.tutorialspoint.com/ruby/ruby_operators.htm
45137
[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/

concepts/conditionals/introduction.md

Lines changed: 93 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,111 @@
1-
# Introduction
1+
# Conditionals
22

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+
## If statement
36+
37+
The [`if`][if] statement is used to check if a given condition is "truthy" or "falsey".
38+
If the condition is truthy then the code inside the if statement will run.
39+
An `if` statement ends with the `end` keyword.
440

541
```ruby
6-
x = 5
42+
value = 1
43+
if value == 1
44+
"1 is equal to 1"
45+
end
46+
# => "1 is equal to 1"
747

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 value > 2
49+
"1 is greater than 2"
50+
end
51+
# => nil
52+
```
53+
54+
## Unless statement
55+
56+
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.
57+
58+
```ruby
59+
value = 1
60+
unless value == 1
61+
"1 is not equal to 1"
62+
end
63+
# => nil
64+
65+
unless value > 2
66+
"1 is not greater than 2"
1467
end
68+
# => "1 is not greater than 2"
1569
```
1670

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:
71+
## Else statement
72+
73+
The `else` statement can be used in conjunction with the `if` and `unless` statements.
74+
The `else` statement will be executed if the `if` branch or the `unless` branch is not executed.
1875

1976
```ruby
20-
x = 4
21-
unless x == 5
22-
# Execute logic if x does not equal 5
77+
value = 1
78+
if value == 1
79+
"1 is equal to 1"
2380
else
24-
# Execute logic if x == 5
81+
"1 is not equal to 1"
82+
end
83+
# => "1 is equal to 1"
84+
85+
unless value < 2
86+
"1 is not greater than 2"
2587
end
88+
# => "1 is greater than 2"
2689
```
2790

28-
If you want to execute different code depending on the value of a variable, Ruby's `case` statement might come useful:
91+
## "Cascading-if" statements
92+
93+
The `elsif` statement can be used in conjunction with the if statement.
94+
The `elsif` statement will be executed if the if branch is not executed and the condition of the elsif statement is truthy.
95+
Elsif statements can be chained together and the first truthy condition will be executed.
96+
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.
2997

3098
```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
99+
value = 1
100+
if value != 1
101+
"1 is not equal to 1"
102+
elsif value > 2
103+
"1 is greater than 2"
37104
else
38-
# Execute logic in all other cases
105+
"1 is not equal to 1 and 1 is not greater than 2"
39106
end
107+
# => "1 is not equal to 1 and 1 is not greater than 2"
40108
```
109+
110+
[comparison-operators]: https://www.w3resource.com/ruby/ruby-comparison-operators.php
111+
[if]: https://www.rubyguides.com/ruby-tutorial/ruby-if-else/

concepts/conditionals/links.json

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
[
2-
{
3-
"url": "https://ruby-doc.org/core-2.7.1/Integer.html",
4-
"description": "integer-ruby"
5-
},
6-
{
7-
"url": "https://ruby-doc.org/core-2.7.1/Float.html",
8-
"description": "float-ruby"
9-
},
10-
{
11-
"url": "https://www.tutorialspoint.com/ruby/ruby_operators.htm",
12-
"description": "arithmetic-operators"
13-
},
142
{
153
"url": "https://www.w3resource.com/ruby/ruby-comparison-operators.php",
164
"description": "comparison-operators"
5+
},
6+
{
7+
"url": "https://www.rubyguides.com/ruby-tutorial/ruby-if-else/",
8+
"description": "Ruby Guides: The Beginner's Guide to Ruby If & Else Statements"
179
}
1810
]

0 commit comments

Comments
 (0)