Skip to content

Commit

Permalink
Update nil and add tag system (#665)
Browse files Browse the repository at this point in the history
* Update nil concept

* Update to new tag system

* Fix instruction file formating
  • Loading branch information
meatball133 authored Aug 7, 2024
1 parent 17e5a78 commit 85685cf
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 32 deletions.
14 changes: 7 additions & 7 deletions concepts/nil/about.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Nil

Crystal has a type which is called [`Nil`][nil], it can only have one value: `nil`.
Crystal has a type called [`Nil`][nil], which can only have one value: `nil`.
It is used to represent the absence of a value, and is similar to `null` or `None` in other languages.

Nil values can be returned from various methods for example [`String#[]?`][string-index], which returns `nil` if the index is out of bounds.
Expand All @@ -10,14 +10,14 @@ Nil values can be returned from various methods for example [`String#[]?`][strin
```

Crystal has what is known as **NULL REFERENCE CHECKS**, which means that all types are non-nilable by default.
This means that you can not assign `nil` to a variable unless you explicitly declare it as a `Nil` type.
This means you can not assign `nil` to a variable unless you explicitly declare it as a `Nil` type.
In turn this means that the compiler will automatically check for null references.

## Falsey value

To be able to handle `nil` values, are there certain approaches that can be taken.
Certain approaches can be taken to handle `nil` values.
`nil` is a [falsey][truthy-falsey] value as well are `false`.
This means that in if statements and other places where a falsey or truthy value is expected it will be treated the same as `false`.
This means that statements and other places where a falsey or truthy value is expected it will be treated the same as `false`.

```crystal
if nil
Expand Down Expand Up @@ -53,12 +53,12 @@ end
```

This is a bit different when just using the value in an if statement, since in the last examples would both false and nil be treated as false.
Here only `nil` is treated as falsy, since if it was false it wouldn't have been nil thereby it would have been truthy.
Here only `nil` is treated as falsey, since if it was false it wouldn't have been nil thereby it would have been truthy.

## Or operator

The easiest way to deal with `nil` values is by ensuring that the value never becomes `nil` in the first place.
The or operator ([`||`][or]) is often used when dealing with `Bools` but if understood correctly it can be used to deal with `nil` values as well.
The easiest way to deal with `nil` values is to ensure that they never become `nil` in the first place.
The or operator ([`||`][or]) is often used when dealing with `Bools`, but if understood correctly, it can also be used to deal with `nil` values.
The or operator checks if the first value is truthy, if not the second value is used.
This can be used to make if the value is `nil` it will be falsey and thereby the second value will be used.

Expand Down
14 changes: 7 additions & 7 deletions concepts/nil/introduction.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Nil

Crystal has a type which is called [`Nil`][nil], it can only have one value: `nil`.
Crystal has a type called [`Nil`][nil], which can only have one value: `nil`.
It is used to represent the absence of a value, and is similar to `null` or `None` in other languages.

Nil values can be returned from various methods for example [`String#[]?`][string-index], which returns `nil` if the index is out of bounds.
Expand All @@ -10,14 +10,14 @@ Nil values can be returned from various methods for example [`String#[]?`][strin
```

Crystal has what is known as **NULL REFERENCE CHECKS**, which means that all types are non-nilable by default.
This means that you can not assign `nil` to a variable unless you explicitly declare it as a `Nil` type.
This means you can not assign `nil` to a variable unless you explicitly declare it as a `Nil` type.
In turn this means that the compiler will automatically check for null references.

## Falsey value

To be able to handle `nil` values, are there certain approaches that can be taken.
Certain approaches can be taken to handle `nil` values.
`nil` is a [falsey][truthy-falsey] value as well are `false`.
This means that in if statements and other places where a falsey or truthy value is expected it will be treated the same as `false`.
This means that statements and other places where a falsey or truthy value is expected it will be treated the same as `false`.

```crystal
if nil
Expand Down Expand Up @@ -53,12 +53,12 @@ end
```

This is a bit different when just using the value in an if statement, since in the last examples would both false and nil be treated as false.
Here only `nil` is treated as falsy, since if it was false it wouldn't have been nil thereby it would have been truthy.
Here only `nil` is treated as falsey, since if it was false it wouldn't have been nil thereby it would have been truthy.

## Or operator

The easiest way to deal with `nil` values is by ensuring that the value never becomes `nil` in the first place.
The or operator ([`||`][or]) is often used when dealing with `Bools` but if understood correctly it can be used to deal with `nil` values as well.
The easiest way to deal with `nil` values is to ensure that they never become `nil` in the first place.
The or operator ([`||`][or]) is often used when dealing with `Bools`, but if understood correctly, it can also be used to deal with `nil` values.
The or operator checks if the first value is truthy, if not the second value is used.
This can be used to make if the value is `nil` it will be falsey and thereby the second value will be used.

Expand Down
16 changes: 8 additions & 8 deletions exercises/concept/castle-dinner/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

Your majesty is hosting a dinner party in the [Kalmar Unions][kalmar_union] capital Copenhagen.
It is the biggest party of the year and all the nobles are invited.
But rumors are spreading across the country that there is a plot to poison your majesty.
But rumors are spreading across the country that there is a plot to poison Your Majesty.

Her majesty has asked you, the royal poison finder, to find out if any of the food or drinks are poisoned.
Her majesty has asked you, the royal poison finder, to find out if any food or drinks are poisoned.
Your majesty is planning to serve only **Mushroom pasties** as food, but for drinks, there are a variety of drinks.

## 1. Check if the food is correct

There is a high chance that the food is poisoned if it is not the food that her majesty ordered.
If the food is not the food that her majesty ordered, then it should be tossed out.
There is a high chance that the food is poisoned if it is not what her majesty ordered.
If the food is not what her majesty ordered, it should be tossed out.

Implement the `check_food?` method, which takes the argument `food` which holds the name of the food as a `String`.
The method should return the food if the food is `"Mushroom pasties"`, otherwise it should return `nil`.
The method should return the food if it is `Mushroom pasties`; otherwise, it should return `nil`.

```crystal
CastleDinner.check_food?("Mushroom pasties")
Expand All @@ -25,7 +25,7 @@ CastleDinner.check_food?("Bread")

## 2. Check if the drink is poisoned

There is a possibility that the drink is poisoned, but it is a bit more difficult to tell if the drink is poisoned.
It is possible that the drink is poisoned, but it is a bit more difficult to tell if it is.
The drink is poisoned if it does **not** include the letter `i` in the name, the casing doesn't matter.
The drink should be tossed out if it is poisoned.

Expand All @@ -43,9 +43,9 @@ CastleDinner.check_drink?("Tea")
## 3. Replace the drink

Your majesty wouldn't want to make their guests worried about poison in their food.
Thereby if the drink is poisoned, it should be replaced with a new drink, specifically **Apple juice**.
Therefore, if the drink is poisoned, it should be replaced with a new drink, specifically **Apple juice**.

Implement the `replace_drink` method, which takes the argument `drink` which holds the name of the drink as a `String`.
Implement the `replace_drink` method, which takes the argument `drink` which holds the drink's name as a `String`.
The method should return the drink if it is not poisoned, otherwise it should return `"Apple juice"`.

```crystal
Expand Down
14 changes: 7 additions & 7 deletions exercises/concept/castle-dinner/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Nil

Crystal has a type which is called [`Nil`][nil], it can only have one value: `nil`.
Crystal has a type called [`Nil`][nil], which can only have one value: `nil`.
It is used to represent the absence of a value, and is similar to `null` or `None` in other languages.

Nil values can be returned from various methods for example [`String#[]?`][string-index], which returns `nil` if the index is out of bounds.
Expand All @@ -10,14 +10,14 @@ Nil values can be returned from various methods for example [`String#[]?`][strin
```

Crystal has what is known as **NULL REFERENCE CHECKS**, which means that all types are non-nilable by default.
This means that you can not assign `nil` to a variable unless you explicitly declare it as a `Nil` type.
This means you can not assign `nil` to a variable unless you explicitly declare it as a `Nil` type.
In turn this means that the compiler will automatically check for null references.

## Falsey value

To be able to handle `nil` values, are there certain approaches that can be taken.
Certain approaches can be taken to handle `nil` values.
`nil` is a [falsey][truthy-falsey] value as well are `false`.
This means that in if statements and other places where a falsey or truthy value is expected it will be treated the same as `false`.
This means that statements and other places where a falsey or truthy value is expected it will be treated the same as `false`.

```crystal
if nil
Expand Down Expand Up @@ -53,12 +53,12 @@ end
```

This is a bit different when just using the value in an if statement, since in the last examples would both false and nil be treated as false.
Here only `nil` is treated as falsy, since if it was false it wouldn't have been nil thereby it would have been truthy.
Here only `nil` is treated as falsey, since if it was false it wouldn't have been nil thereby it would have been truthy.

## Or operator

The easiest way to deal with `nil` values is by ensuring that the value never becomes `nil` in the first place.
The or operator ([`||`][or]) is often used when dealing with `Bools` but if understood correctly it can be used to deal with `nil` values as well.
The easiest way to deal with `nil` values is to ensure that they never become `nil` in the first place.
The or operator ([`||`][or]) is often used when dealing with `Bools`, but if understood correctly, it can also be used to deal with `nil` values.
The or operator checks if the first value is truthy, if not the second value is used.
This can be used to make if the value is `nil` it will be falsey and thereby the second value will be used.

Expand Down
6 changes: 3 additions & 3 deletions exercises/concept/castle-dinner/spec/castle_dinner_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require "spec"
require "../src/*"

describe CastleDinner do
describe "check_food?" do
describe "check_food?", tags: "task_id=1" do
it "should return food if it is Mushroom pasties" do
CastleDinner.check_food?("Mushroom pasties").should eq "Mushroom pasties"
end
Expand All @@ -16,7 +16,7 @@ describe CastleDinner do
end
end

describe "check_drink?" do
describe "check_drink?", tags: "task_id=2" do
it "should return drink if it is Apple jucie" do
CastleDinner.check_drink?("Apple juice").should eq "Apple juice"
end
Expand All @@ -38,7 +38,7 @@ describe CastleDinner do
end
end

describe "replace_drink" do
describe "replace_drink", tags: "task_id=3" do
it "shouldnt replace Elderberry juice" do
CastleDinner.replace_drink("Elderberry juice").should eq "Elderberry juice"
end
Expand Down

0 comments on commit 85685cf

Please sign in to comment.