From 250cb676e5c8b1d8f5960579e82b85f9d873923b Mon Sep 17 00:00:00 2001 From: Carl Date: Tue, 7 Nov 2023 19:24:58 +0100 Subject: [PATCH] Refactor conditionals in Ruby concepts and add new links --- concepts/conditionals/.meta/config.json | 4 +- concepts/conditionals/about.md | 146 ++++++++++++++---- concepts/conditionals/introduction.md | 117 +++++++++++--- concepts/conditionals/links.json | 16 +- .../assembly-line/.docs/introduction.md | 113 +++++++++++++- 5 files changed, 325 insertions(+), 71 deletions(-) diff --git a/concepts/conditionals/.meta/config.json b/concepts/conditionals/.meta/config.json index 48fc01e723..01415847c6 100644 --- a/concepts/conditionals/.meta/config.json +++ b/concepts/conditionals/.meta/config.json @@ -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"] } diff --git a/concepts/conditionals/about.md b/concepts/conditionals/about.md index 7cbdd31242..681dd54c29 100644 --- a/concepts/conditionals/about.md +++ b/concepts/conditionals/about.md @@ -1,48 +1,140 @@ -# About +# Conditionals -An `if` statement can be used to conditionally execute code: +Ruby has what is known as 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 +``` + +## 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" +else + "1 is 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/ diff --git a/concepts/conditionals/introduction.md b/concepts/conditionals/introduction.md index af5ad14679..dfb64e7ce1 100644 --- a/concepts/conditionals/introduction.md +++ b/concepts/conditionals/introduction.md @@ -1,40 +1,113 @@ -# Introduction +# Conditionals -An `if` statement can be used to conditionally execute code: +Ruby has what is known as 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 ``` -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 + "1 is not equal to 1" +end +# => "1 is equal to 1" + +unless value < 2 + "1 is not greater than 2" else - # Execute logic if x == 5 + "1 is 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/ diff --git a/concepts/conditionals/links.json b/concepts/conditionals/links.json index a58c1c1b77..f5b149913e 100644 --- a/concepts/conditionals/links.json +++ b/concepts/conditionals/links.json @@ -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" } ] diff --git a/exercises/concept/assembly-line/.docs/introduction.md b/exercises/concept/assembly-line/.docs/introduction.md index 5fd0b26fc1..488630d21f 100644 --- a/exercises/concept/assembly-line/.docs/introduction.md +++ b/exercises/concept/assembly-line/.docs/introduction.md @@ -18,17 +18,114 @@ These classes have methods that will coerce values from one to the other. `Integ ## Conditionals -In this exercise you must conditionally execute logic. -A common way to do this in Ruby is by using an `if/else` statement: +Ruby has what is known as 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 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 -if x == 5 - # Execute logic if x equals 5 -elsif x > 7 - # Execute logic if x greater than 7 +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 + "1 is not equal to 1" +end +# => "1 is equal to 1" + +unless value < 2 + "1 is not greater than 2" else - # Execute logic in all other cases + "1 is greater than 2" end +# => "1 is greater than 2" ``` + +## "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 +value = 1 +if value != 1 + "1 is not equal to 1" +elsif value > 2 + "1 is greater than 2" +else + "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/