Skip to content

Commit 407f3fe

Browse files
meatball133kotp
andauthored
[New concept + exercise]: Case (#1595)
* Add case concept + exercise * Add new line in end of exemplar * Fix * Add missing spacing * Fixes based on feedback * remove trailing whitespace * Add contributor to config.json file --------- Co-authored-by: Victor Goff <[email protected]>
1 parent 94cde87 commit 407f3fe

File tree

13 files changed

+862
-0
lines changed

13 files changed

+862
-0
lines changed

concepts/case/.meta/config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"blurb": "Case is a form of control expression, and allows for writing large readable if-else-if statements.",
3+
"authors": ["meatball133"],
4+
"contributors": ["kotp"]
5+
}

concepts/case/about.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Case
2+
3+
[Case][case] (often referred to as switch in other languages) is a form of control expression like if-else.
4+
Case allows for chaining of multiple if-else-if statements and can be more readable while still providing flow control.
5+
6+
A case is defined by the keyword `case` followed by an optional expression.
7+
Then for each case, the keyword `when` is used followed by an expression which is compared to the case expression.
8+
The `when` keyword should not be indented from the `case` keyword.
9+
After the `when` keyword is the code that should be executed if the case expression matches the when expression.
10+
Case allows for an optional `else` statement which is executed if no other case matches.
11+
12+
The case expression is evaluated and then compared to each `when` expression.
13+
The expression is compared using the case equality operator (`===`).
14+
15+
```ruby
16+
value = 1
17+
case value
18+
when 1
19+
"One"
20+
when 2
21+
"Two"
22+
else
23+
"Other"
24+
end
25+
26+
# This is the same as:
27+
value = 1
28+
if 1 === value
29+
"One"
30+
elsif 2 === value
31+
"Two"
32+
else
33+
"Other"
34+
end
35+
```
36+
37+
## Case equality operator (`===`)
38+
39+
The case equality operator (`===`) is a bit different from the equality operator (`==`).
40+
The operator checks if the right side is a member of the set described by the left side.
41+
This means that it does matter where each operand is placed.
42+
How this works depends on the type of the left side, for example a `Range` would check if the right side is in the range or a `Object` would check if the right side is an instance of the `Object`.
43+
44+
```ruby
45+
(1..3) == 1 # => false
46+
(1..3) === 1 # => true
47+
48+
String == "foo" # => false
49+
String === "foo" # => true
50+
```
51+
52+
## Case with multiple expressions
53+
54+
Cases allow for matching multiple expressions in a single case with each possible value separated by a comma.
55+
It will execute the code if any of the expressions match.
56+
This can be useful when you want a single case to have multiple possible values.
57+
58+
```ruby
59+
case var
60+
when 1, 2
61+
"One or two"
62+
else
63+
"Other"
64+
end
65+
```
66+
67+
## Cases with ranges
68+
69+
Cases can also check if a value is in a range.
70+
This is done by having a range as the when expression.
71+
72+
```ruby
73+
case var
74+
when 1..3
75+
puts "One to three"
76+
else
77+
puts "Other"
78+
end
79+
```
80+
81+
## Cases with no case expression
82+
83+
When there is no need for a case expression, it is possible to omit it.
84+
Doing this will make it so that each case expression is evaluated for truthiness.
85+
And makes them behave like if-else-if statements.
86+
87+
```ruby
88+
case
89+
when 1 == 1
90+
"One is equal to one"
91+
when 1 > 2
92+
"One is greater than two"
93+
else
94+
"Other"
95+
end
96+
```
97+
98+
## Single line when
99+
100+
Ruby allows for single line case statements.
101+
This can be used when you have a simple single line statement.
102+
The single line when statement is written as `when <expression> then <statement>`.
103+
And when used in the else statement it is written as `else <statement>`.
104+
105+
```ruby
106+
case var
107+
when 1 then "One"
108+
when 2 then "Two"
109+
else "Other"
110+
end
111+
```
112+
113+
## Case with types
114+
115+
Case allows for the matching with types.
116+
This is useful when wanting different behavior depending on the type of a variable.
117+
118+
```ruby
119+
case var
120+
when Integer
121+
"Integer"
122+
when String
123+
"String"
124+
else
125+
"Other"
126+
end
127+
```
128+
129+
[case]: https://www.rubyguides.com/2015/10/ruby-case/

concepts/case/introduction.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Case
2+
3+
[Case][case] (often referred to as switch in other languages) is a form of control expression like if-else.
4+
Case allows for chaining of multiple if-else-if statements and can be more readable while still providing flow control.
5+
6+
A case is defined by the keyword `case` followed by an optional expression.
7+
Then for each case, the keyword `when` is used followed by an expression which is compared to the case expression.
8+
The `when` keyword should not be indented from the `case` keyword.
9+
After the `when` keyword is the code that should be executed if the case expression matches the when expression.
10+
Case allows for an optional `else` statement which is executed if no other case matches.
11+
12+
The case expression is evaluated and then compared to each `when` expression.
13+
The expression is compared using the case equality operator (`===`).
14+
15+
```ruby
16+
value = 1
17+
case value
18+
when 1
19+
"One"
20+
when 2
21+
"Two"
22+
else
23+
"Other"
24+
end
25+
26+
# This is the same as:
27+
value = 1
28+
if 1 === value
29+
"One"
30+
elsif 2 === value
31+
"Two"
32+
else
33+
"Other"
34+
end
35+
```
36+
37+
## Case equality operator (`===`)
38+
39+
The case equality operator (`===`) is a bit different from the equality operator (`==`).
40+
The operator checks if the right side is a member of the set described by the left side.
41+
This means that it does matter where each operand is placed.
42+
How this works depends on the type of the left side, for example a `Range` would check if the right side is in the range or a `Object` would check if the right side is an instance of the `Object`.
43+
44+
```ruby
45+
(1..3) == 1 # => false
46+
(1..3) === 1 # => true
47+
48+
String == "foo" # => false
49+
String === "foo" # => true
50+
```
51+
52+
## Case with multiple expressions
53+
54+
Cases allow for matching multiple expressions in a single case with each possible value separated by a comma.
55+
It will execute the code if any of the expressions match.
56+
This can be useful when you want a single case to have multiple possible values.
57+
58+
```ruby
59+
case var
60+
when 1, 2
61+
"One or two"
62+
else
63+
"Other"
64+
end
65+
```
66+
67+
## Cases with ranges
68+
69+
Cases can also check if a value is in a range.
70+
This is done by having a range as the when expression.
71+
72+
```ruby
73+
case var
74+
when 1..3
75+
puts "One to three"
76+
else
77+
puts "Other"
78+
end
79+
```
80+
81+
## Cases with no case expression
82+
83+
When there is no need for a case expression, it is possible to omit it.
84+
Doing this will make it so that each case expression is evaluated for truthiness.
85+
And makes them behave like if-else-if statements.
86+
87+
```ruby
88+
case
89+
when 1 == 1
90+
"One is equal to one"
91+
when 1 > 2
92+
"One is greater than two"
93+
else
94+
"Other"
95+
end
96+
```
97+
98+
## Single line when
99+
100+
Ruby allows for single line case statements.
101+
This can be used when you have a simple single line statement.
102+
The single line when statement is written as `when <expression> then <statement>`.
103+
And when used in the else statement it is written as `else <statement>`.
104+
105+
```ruby
106+
case var
107+
when 1 then "One"
108+
when 2 then "Two"
109+
else "Other"
110+
end
111+
```
112+
113+
## Case with types
114+
115+
Case allows for the matching with types.
116+
This is useful when wanting different behavior depending on the type of a variable.
117+
118+
```ruby
119+
case var
120+
when Integer
121+
"Integer"
122+
when String
123+
"String"
124+
else
125+
"Other"
126+
end
127+
```
128+
129+
[case]: https://www.rubyguides.com/2015/10/ruby-case/

concepts/case/links.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"url": "https://www.rubyguides.com/2015/10/ruby-case/",
4+
"description": "Ruby Guides: The Many Uses Of Ruby Case Statements"
5+
}
6+
]

config.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@
129129
"symbols"
130130
]
131131
},
132+
{
133+
"slug": "blackjack",
134+
"name": "BlackJack",
135+
"uuid": "414ee9b2-c62d-41d5-aa1e-c5d82fb8562c",
136+
"concepts": [
137+
"case"
138+
],
139+
"prerequisites": [
140+
"ranges",
141+
"modules"
142+
]
143+
},
132144
{
133145
"slug": "bird-count",
134146
"name": "Bird Count",
@@ -1702,6 +1714,11 @@
17021714
"slug": "modules",
17031715
"name": "Modules"
17041716
},
1717+
{
1718+
"uuid": "bcea6b47-4db8-4cce-b341-c2f97182687b",
1719+
"slug": "case",
1720+
"name": "Case"
1721+
},
17051722
{
17061723
"uuid": "ad546bc1-7583-482d-9159-44e08c50c7b8",
17071724
"slug": "exceptions",
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Hints
2+
3+
## 1. Calculate the value of any given card.
4+
5+
- The `Blackjack.parse_card` method should take a string (e.g. `"ace"`) and return its value (e.g. 11).
6+
- Define a [`case` statement][case] and assign the value of the card as the case's value.
7+
- King, Queen, Jack and 10 can be handled with a single case.
8+
- The `case` can have an `else` case.
9+
In any case the function should return `0` for unknown cards.
10+
11+
## 2. Name ranges of values.
12+
13+
- Compute the player score by adding up the values of the two player cards.
14+
- Define a [`case` statement][case] and assign the name of the range as the case's value.
15+
- The switch can use [`Range`][range] objects as cases to check if a value is in a range.
16+
17+
## 3. Implement the decision logic for the first turn.
18+
19+
- Compute the player score by adding up the values of the two player cards.
20+
- You can either use a big [`case` statement][case] on the player
21+
score (maybe with nested `if`-statements on the dealer-score in some cases),
22+
- or you could distinguish separate player score categories (say "small hands"
23+
with a score less than 12, "medium hands" with a score in the range 12..20 and
24+
"large hands" with a score greater than 20) and write separate functions for
25+
all (or some) of these categories.
26+
27+
[case]: https://www.rubyguides.com/2015/10/ruby-case/
28+
[range]: https://rubyapi.org/o/range

0 commit comments

Comments
 (0)