Skip to content

Commit

Permalink
[Refactor Concept]: conditionals (#1597)
Browse files Browse the repository at this point in the history
* Refactor conditionals in Ruby concepts and add new links

* Changes based on feedback
  • Loading branch information
meatball133 authored Nov 17, 2023
1 parent 8a8da22 commit 315e08b
Show file tree
Hide file tree
Showing 5 changed files with 319 additions and 71 deletions.
4 changes: 2 additions & 2 deletions concepts/conditionals/.meta/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"blurb": "Ruby has several constructs to conditionally execute code: if statements, unless statements and case statements.",
"authors": ["dvik1950"],
"blurb": "Ruby has conditionals to control the flow of your program. You can use if, unless to control the flow of your program.",
"authors": ["dvik1950", "meatball133"],
"contributors": ["kotp", "iHiD"]
}
144 changes: 117 additions & 27 deletions concepts/conditionals/about.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,138 @@
# About
# Conditionals

An `if` statement can be used to conditionally execute code:
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.
There are operators that can be used to create truthy or falsey values, these are known as [comparison operators][comparison-operators].

There are two main control expressions that are used to control which code will run and which will not.
Also known as which given branch will run.

Those two are: `if` and the `unless` expression.

## Comparison operators

[Comparison operators][comparison-operators] are used to compare values and return a `true` or `false` value.
The following operators require two values to be compared of the same type.
If the values are not of the same type then the compiler will throw an error.
Here is a list of the operators and an example of when they give a `true` value:

| Method | Description | Example |
| ------ | --------------------- | ------- |
| < | less than | 5 < 4 |
| <= | less than or equal | 4 <= 4 |
| > | greater than | 3 > 1 |
| >= | greater than or equal | 2 >= 2 |

The equal and not equal operators can be used to compare any type of value contrary to the operators already mentioned.
The `==` operator is used to check if two values are equal, and that includes checking the type of the value.
The `!=` works the same way but it will return `true` if the values are not equal and `false` if they are equal.
Here is a list of the equal and not equal operators and an example of when they give a `true` value:

| Method | Description | Example |
| ------ | ------------ | ------- |
| == | equal | 4 == 4 |
| != | not equal | 5 != 4 |

## Combined comparison operator

The combined comparison operator (sometimes called spaceship operator) is a special comparison operator.
It is special in the sense that it doesn't return a truthy or falsey value but it returns a number.
It is written as `<=>` and it is used to compare 2 values.
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.

```ruby
x = 5
1 <=> 2 # => -1
2 <=> 2 # => 0
3 <=> 2 # => 1
```

if x == 5
# Execute logic if x equals 5
elsif x > 7
# Execute logic if x greater than 7
else
# Execute logic in all other cases
## If statement

The [`if`][if] statement is used to check if a given condition is "truthy" or "falsey".
If the condition is truthy then the code inside the if statement will run.
An `if` statement ends with the `end` keyword.

```ruby
value = 1
if value == 1
"1 is equal to 1"
end
# => "1 is equal to 1"

if value > 2
"1 is greater than 2"
end
# => nil
```

Sometimes you want to execute a statement (or statements) if a condition is _not_ true, for situations like that, Ruby implements the `unless` keyword:
## Unless statement

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.

```ruby
x = 4
unless x == 5
# Execute logic if x does not equal 5
value = 1
unless value == 1
"1 is not equal to 1"
end
# => nil

unless value > 2
"1 is not greater than 2"
end
# => "1 is not greater than 2"
```

## Else statement

The `else` statement can be used in conjunction with the `if` and `unless` statements.
The `else` statement will be executed if the `if` branch or the `unless` branch is not executed.

```ruby
value = 1
if value == 1
"1 is equal to 1"
else
# Execute logic if x == 5
"1 is not equal to 1"
end
# => "1 is equal to 1"

unless value < 2
"1 is not greater than 2"
end
# => "1 is greater than 2"
```

If you want to execute different code depending on the value of a variable, Ruby's `case` statement might come useful:
## "Cascading-if" statements

The `elsif` statement can be used in conjunction with the if statement.
The `elsif` statement will be executed if the if branch is not executed and the condition of the elsif statement is truthy.
Elsif statements can be chained together and the first truthy condition will be executed.
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.

```ruby
y = 5
case y
when 3
# Execute logic if y equals 3
when 5
# Execute logic if y equals 5
value = 1
if value != 1
"1 is not equal to 1"
elsif value > 2
"1 is greater than 2"
else
# Execute logic in all other cases
"1 is not equal to 1 and 1 is not greater than 2"
end
# => "1 is not equal to 1 and 1 is not greater than 2"
```

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.
## if and unless as suffix

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.
It is done by putting the if or unless statement after the code that you want to run.

```ruby
value = 1
"1 is equal to 1" if value == 1
# => 1 is equal to 1

"1 is not equal to 1" unless value == 1
# => nil
```

[arithmetic-operators]: https://www.tutorialspoint.com/ruby/ruby_operators.htm
[comparison-operators]: https://www.w3resource.com/ruby/ruby-comparison-operators.php
[if-else-unless]: https://www.w3resource.com/ruby/ruby-if-else-unless.php
[integer-ruby]: https://ruby-doc.org/core-2.7.1/Integer.html
[float-ruby]: https://ruby-doc.org/core-2.7.1/Float.html
[if]: https://www.rubyguides.com/ruby-tutorial/ruby-if-else/
115 changes: 93 additions & 22 deletions concepts/conditionals/introduction.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,111 @@
# Introduction
# Conditionals

An `if` statement can be used to conditionally execute code:
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.
There are operators that can be used to create truthy or falsey values, these are known as [comparison operators][comparison-operators].

There are two main control expressions that are used to control which code will run and which will not.
Also known as which given branch will run.

Those two are: `if` and the `unless` expression.

## Comparison operators

[Comparison operators][comparison-operators] are used to compare values and return a `true` or `false` value.
The following operators require two values to be compared of the same type.
If the values are not of the same type then the compiler will throw an error.
Here is a list of the operators and an example of when they give a `true` value:

| Method | Description | Example |
| ------ | --------------------- | ------- |
| < | less than | 5 < 4 |
| <= | less than or equal | 4 <= 4 |
| > | greater than | 3 > 1 |
| >= | greater than or equal | 2 >= 2 |

The equal and not equal operators can be used to compare any type of value contrary to the operators already mentioned.
The `==` operator is used to check if two values are equal, and that includes checking the type of the value.
The `!=` works the same way but it will return `true` if the values are not equal and `false` if they are equal.
Here is a list of the equal and not equal operators and an example of when they give a `true` value:

| Method | Description | Example |
| ------ | ------------ | ------- |
| == | equal | 4 == 4 |
| != | not equal | 5 != 4 |

## If statement

The [`if`][if] statement is used to check if a given condition is "truthy" or "falsey".
If the condition is truthy then the code inside the if statement will run.
An `if` statement ends with the `end` keyword.

```ruby
x = 5
value = 1
if value == 1
"1 is equal to 1"
end
# => "1 is equal to 1"

if x == 5
# Execute logic if x equals 5
elsif x > 7
# Execute logic if x greater than 7
else
# Execute logic in all other cases
if value > 2
"1 is greater than 2"
end
# => nil
```

## Unless statement

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.

```ruby
value = 1
unless value == 1
"1 is not equal to 1"
end
# => nil

unless value > 2
"1 is not greater than 2"
end
# => "1 is not greater than 2"
```

Sometimes you want to execute a statement (or statements) if a condition is _not_ true, for situations like that, Ruby implements the `unless` keyword:
## Else statement

The `else` statement can be used in conjunction with the `if` and `unless` statements.
The `else` statement will be executed if the `if` branch or the `unless` branch is not executed.

```ruby
x = 4
unless x == 5
# Execute logic if x does not equal 5
value = 1
if value == 1
"1 is equal to 1"
else
# Execute logic if x == 5
"1 is not equal to 1"
end
# => "1 is equal to 1"

unless value < 2
"1 is not greater than 2"
end
# => "1 is greater than 2"
```

If you want to execute different code depending on the value of a variable, Ruby's `case` statement might come useful:
## "Cascading-if" statements

The `elsif` statement can be used in conjunction with the if statement.
The `elsif` statement will be executed if the if branch is not executed and the condition of the elsif statement is truthy.
Elsif statements can be chained together and the first truthy condition will be executed.
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.

```ruby
y = 5
case y
when 3
# Execute logic if y equals 3
when 5
# Execute logic if y equals 5
value = 1
if value != 1
"1 is not equal to 1"
elsif value > 2
"1 is greater than 2"
else
# Execute logic in all other cases
"1 is not equal to 1 and 1 is not greater than 2"
end
# => "1 is not equal to 1 and 1 is not greater than 2"
```

[comparison-operators]: https://www.w3resource.com/ruby/ruby-comparison-operators.php
[if]: https://www.rubyguides.com/ruby-tutorial/ruby-if-else/
16 changes: 4 additions & 12 deletions concepts/conditionals/links.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
[
{
"url": "https://ruby-doc.org/core-2.7.1/Integer.html",
"description": "integer-ruby"
},
{
"url": "https://ruby-doc.org/core-2.7.1/Float.html",
"description": "float-ruby"
},
{
"url": "https://www.tutorialspoint.com/ruby/ruby_operators.htm",
"description": "arithmetic-operators"
},
{
"url": "https://www.w3resource.com/ruby/ruby-comparison-operators.php",
"description": "comparison-operators"
},
{
"url": "https://www.rubyguides.com/ruby-tutorial/ruby-if-else/",
"description": "Ruby Guides: The Beginner's Guide to Ruby If & Else Statements"
}
]
Loading

0 comments on commit 315e08b

Please sign in to comment.