-
-
Notifications
You must be signed in to change notification settings - Fork 519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[New concept + exercise]: Case #1595
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "same" example behaves differently than the other example.
"Case equality" is used pretty consistently over the years for the ===
method as used in a case statement.
@@ -0,0 +1,129 @@ | |||
# Case | |||
|
|||
[Case][case] (often referred to as switch in other languages) is a form of control expression like if-else. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Case][case] (often referred to as switch in other languages) is a form of control expression like if-else. | |
[Case][case] (often referred to as switch in other languages) is a form of flow control expression like if-else. |
concepts/case/about.md
Outdated
# Case | ||
|
||
[Case][case] (often referred to as switch in other languages) is a form of control expression like if-else. | ||
Case allows for chaining of multiple if-else-if statements and can be more readable and also allows for powerful constructs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Case allows for chaining of multiple if-else-if statements and can be more readable and also allows for powerful constructs. | |
Case allows for chaining of multiple `when` statements and can be more readable and also allows for powerful constructs. |
Also, does it really allow for "more powerful constructs"? Or are they just as powerful but different?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesnt really say they are more powerful just that they allow for powerful constructs.
And it is suppose to be if-else-if.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see what it says, and yes, not "more" powerful but even "powerful"? It is comparing if to case statements, and suggests that case statements allow for powerful constructs, though if statements also do, so I am kind of "reading into" the "more" since it seems to be a comparison. But should it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And, yes, while we can indeed use if-else-if along and even inside of case statements, the case statement does not itself use that syntax.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would even say that it is allowed for, even without case. So the case statement isn't what is allowing for chaining if-else-if statements.
Then for each case, the keyword `when` is used followed by an expression which is compared to the case expression. | ||
The `when` keyword should not be indented from the `case` keyword. | ||
After the `when` keyword is the code that should be executed if the case expression matches the when expression. | ||
Case allows for an optional `else` statement which is executed if no other case matches. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Case allows for an optional `else` statement which is executed if no other case matches. | |
Case allows for an optional `else` statement which is executed if no other `when` matches. |
concepts/case/about.md
Outdated
Case allows for an optional `else` statement which is executed if no other case matches. | ||
|
||
The case expression is evaluated and then compared to each `when` expression. | ||
The expression is compared using the case subsumption operator (`===`). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Ruby, we call that the "case equality operator".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have seen Jorg (from stack overflow) use it in 2010 and someone else call it this in their blog in 2022, but this is only the third time I have seen this ===
called that.
concepts/case/about.md
Outdated
The expression is compared using the case subsumption operator (`===`). | ||
|
||
```ruby | ||
case 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
case 1 | |
case value |
concepts/case/about.md
Outdated
# Case | ||
|
||
[Case][case] (often referred to as switch in other languages) is a form of control expression like if-else. | ||
Case allows for chaining of multiple if-else-if statements and can be more readable and also allows for powerful constructs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Case allows for chaining of multiple if-else-if statements and can be more readable and also allows for powerful constructs. | |
Case allows for chaining of multiple if-else-if statements and can be more readable while still providing flow control. |
How about that?
The goal of this concept is to dig deeper into case, and in a later pr remove
case
from conditionals and expand the conditionals concept with more info.