Skip to content

Commit

Permalink
Updated Book of Ryan to include template strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Arruda committed Aug 12, 2023
1 parent fa0c6e1 commit 418abbc
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion book-of-ryan/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# The Art of Ryan

- [Hello... Jason!?](./art-of-ryan/hello-json.md)
- [Hello... JSON!?](./art-of-ryan/hello-json.md)
- [Simple types](./art-of-ryan/simple-types.md)
- [Variables](./art-of-ryan/variables.md)
- [Not-so-simple types](./art-of-ryan/not-so-simple-types.md)
Expand Down
8 changes: 7 additions & 1 deletion book-of-ryan/src/art-of-ryan/hello-json.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Hello... Jason!?
# Hello... JSON!?

> Note: if you are unfamiliar with JSON, you may skip this chapter and go right ahead to the next one. You will still be able to understand what Ryan is all about.
Expand Down Expand Up @@ -56,6 +56,12 @@ are allowed!"
```
... given that they are valid UTF8. However, only double-quoted strings are possible. How could you possibly like those single-quotes aberrations?

Finally, _template strings_ à la JavaScript are also supported. These will make your code way simpler to read:
```
`template ${"string"}`
```
The above code evaluates to the string `"template string"`.

## Conclusion to the introduction

You might be asking yourself about all the cosmetics and nice gadgets from the last section: "so what? JSON5 offers basically the same thing and YAML does that and _tons_ more. What is the upside here?" Well, Ryan is capable of much more than these simple tricks and that is what we are going to explore in the following chapters. However, if all you got from this book is that Ryan is a nicer JSON, well... that's a start (and _definitely_ a legitimate use for Ryan).
5 changes: 5 additions & 0 deletions book-of-ryan/src/art-of-ryan/importing.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ Although `import` expects a string as input, you cannot use an expression that y
```ryan
import "my-" + "file.ryan"
```
nor will this
```
let num = 4;
import `file-${num}.ryan`
```
You can however, go around this limitation in many cases. For example, you can use `if ... then ... else ...` for conditional imports:
```ryan
if 1 == 1 then
Expand Down
4 changes: 2 additions & 2 deletions book-of-ryan/src/art-of-ryan/pattern-matching.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ You can use any pattern to declare a pattern match.
All pattern matches in Ryan are _closures_. That means that you are free to use variables defined outside the pattern match definition in your return expression:
```ryan
let object_name = "lights";
let there_are quantity = "There are " + fmt quantity + " " + object_name;
let there_are quantity = `There are ${quantity} ${object_name}`;
there_are 4 // -> "There are 4 lights"
```
Expand All @@ -83,7 +83,7 @@ A pattern match does not expect only an expression, but a whole block. This mean
```ryan
let there_are quantity =
let object_name = "lights";
"There are " + fmt quantity + " " + object_name;
`There are ${quantity} ${object_name}`;
there_are 4 // -> "There are 4 lights"
```
Expand Down
9 changes: 5 additions & 4 deletions book-of-ryan/src/art-of-ryan/simple-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ However, you can use the "`fmt` trick" to achieve the desired result:
```ryan
"there are " + fmt 4 + " lights" // there are 4 lights
```
The `fmt`... thingy... takes any value in Ryan and produces a string representation of it, even if it is not very useful.

> Note: `fmt` is called a native pattern match and will be introduced later.
Or, better still, you can use _template strings_, where you can interpolate full expressions in the middle of your string, like so:
```ryan
`there are ${5 - 1} lights` // there are 4 lights
```
Note that template strings, unlike normal strings, are escaped using \` and not `"`.

## `null`

Expand Down
4 changes: 2 additions & 2 deletions book-of-ryan/src/art-of-ryan/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ Every time you declare a new variable in a pattern match, you can define an opti

Things get fun when you can bring _polymorphism_ to your pattern matches by powering type guards with _alternative patterns_:
```ryan
let foo x: int = "I am an integer: " + fmt x;
let foo x: float = "I am a float: " + fmt x;
let foo x: int = `I am an integer: ${x}`;
let foo x: float = `I am a float: ${x}`;
[foo 1, foo 1.0] // -> ["I am an integer: 1", "I am a float: 1"]
```

Expand Down
2 changes: 1 addition & 1 deletion book-of-ryan/src/art-of-ryan/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ let a_guy = "Ryan";
What this line says is that `a_guy` refers to the value `"Ryan"` from now on. Now, you can start using it to build more complex stuff on top of it:
```ryan
let a_guy = "Ryan";
"Hello, " + a_guy + "!"
`Hello, ${a_guy}!`
```
This will evaluate to the text `"Hello, Ryan!"`. The construction `let ... = ...;` is called a _variable binding_, becaus it binds the value (`"Ryan"`) to an identifier (`a_guy`).

Expand Down

0 comments on commit 418abbc

Please sign in to comment.