docs: document string interpolation syntax and update code examples across docs and site

Add a new "Interpolation" section to the values documentation explaining the `%(expr)` syntax with examples of complex expressions and nested interpolation. Update all code samples in README.md, getting-started.markdown, and index.markdown to use the new interpolation syntax instead of string concatenation. Adjust syntax highlighting in style.scss by adding a new `.si` class for interpolation spans with distinct background color, and tweak existing color values for strings, numbers, and keywords to improve visual distinction. Add the `\%` escape sequence to the list of supported escape characters in values.markdown.
This commit is contained in:
Bob Nystrom
2015-11-21 17:20:50 +00:00
parent 920b3e2a32
commit fa1c1f5cda
5 changed files with 29 additions and 7 deletions
+19
View File
@@ -49,6 +49,7 @@ A handful of escape characters are supported:
"\0" // The NUL byte: 0.
"\"" // A double quote character.
"\\" // A backslash.
"\%" // A percent sign.
"\a" // Alarm beep. (Who uses this?)
"\b" // Backspace.
"\f" // Formfeed.
@@ -77,6 +78,24 @@ Strings are instances of class [String][].
[string]: modules/core/string.html
### Interpolation
String literals also allow *interpolation*. If you have a percent sign (`%`)
followed by a parenthesized expression, the expression is evaluated. The
resulting object's `toString` method is called and the result is inserted in the
string:
:::wren
System.print("Math %(3 + 4 * 5) is fun!") //> Math 23 is fun!
Arbitrarily complex expressions are allowed inside the parentheses:
:::wren
System.print("wow %((1..3).map {|n| n * n}.join())") //> wow 149
An interpolated expression can even contain a string literal which in turn has
its own nested intpolations, but doing that gets unreadable pretty quickly.
## Ranges
A range is a little object that represents a consecutive range of numbers. They