From a80a0bc4e6e4b55293aed56ac788e824aeb88c6b Mon Sep 17 00:00:00 2001 From: Otto Chrons Date: Wed, 3 Jan 2018 11:42:50 +0200 Subject: [PATCH] Add support for direct ScalaFiddle integration into document code examples. --- Gemfile | 2 ++ Gemfile.lock | 5 ++++- _config.yml | 1 + _tour/basics.md | 15 +++++++++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 896a75036c..fbdf46298f 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,7 @@ source 'https://rubygems.org' gem 'jekyll-redirect-from' +gem 'jekyll-scalafiddle' + # gem 'html-proofer' # link-checking: bundle exec htmlproofer ./_site/ --only-4xx --empty-alt-ignore --allow-hash-href # group :jekyll_plugins do diff --git a/Gemfile.lock b/Gemfile.lock index 122d727d68..de5fa07c17 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -21,6 +21,8 @@ GEM jekyll (~> 3.3) jekyll-sass-converter (1.5.0) sass (~> 3.4) + jekyll-scalafiddle (1.0.1) + jekyll (~> 3.0) jekyll-watch (1.5.0) listen (~> 3.0, < 3.1) kramdown (1.14.0) @@ -48,6 +50,7 @@ PLATFORMS DEPENDENCIES jekyll-redirect-from + jekyll-scalafiddle BUNDLED WITH - 1.15.4 + 1.16.1 diff --git a/_config.yml b/_config.yml index da1e6599c1..b04fd8d4bd 100644 --- a/_config.yml +++ b/_config.yml @@ -84,3 +84,4 @@ baseurl: exclude: ["vendor"] plugins: - jekyll-redirect-from + - jekyll-scalafiddle diff --git a/_tour/basics.md b/_tour/basics.md index 37510230f7..e9ee360344 100644 --- a/_tour/basics.md +++ b/_tour/basics.md @@ -25,6 +25,9 @@ You can run Scala in your browser with ScalaFiddle. This is an easy, zero-setup way to experiment with pieces of Scala code. +Many of the code examples in this documentation are also integrated with ScalaFiddle, so you +can directly experiment with them simply by clicking the Run-button. + ## Expressions Expressions are computable statements. @@ -33,12 +36,14 @@ Expressions are computable statements. ``` You can output results of expressions using `println`. +{% scalafiddle %} ```tut println(1) // 1 println(1 + 1) // 2 println("Hello!") // Hello! println("Hello," + " world!") // Hello, world! ``` +{% endscalafiddle %} ### Values @@ -111,17 +116,21 @@ On the left of `=>` is a list of parameters. On the right is an expression invol You can also name functions. +{% scalafiddle %} ```tut val addOne = (x: Int) => x + 1 println(addOne(1)) // 2 ``` +{% endscalafiddle %} Functions may take multiple parameters. +{% scalafiddle %} ```tut val add = (x: Int, y: Int) => x + y println(add(1, 2)) // 3 ``` +{% endscalafiddle %} Or it can take no parameters. @@ -136,19 +145,23 @@ Methods look and behave very similar to functions, but there are a few key diffe Methods are defined with the `def` keyword. `def` is followed by a name, parameter lists, a return type, and a body. +{% scalafiddle %} ```tut def add(x: Int, y: Int): Int = x + y println(add(1, 2)) // 3 ``` +{% endscalafiddle %} Notice how the return type is declared _after_ the parameter list and a colon `: Int`. Methods can take multiple parameter lists. +{% scalafiddle %} ```tut def addThenMultiply(x: Int, y: Int)(multiplier: Int): Int = (x + y) * multiplier println(addThenMultiply(1, 2)(3)) // 9 ``` +{% endscalafiddle %} Or no parameter lists at all. @@ -266,6 +279,7 @@ trait Greeter { Traits can also have default implementations. +{% scalafiddle %} ```tut trait Greeter { def greet(name: String): Unit = @@ -290,6 +304,7 @@ greeter.greet("Scala developer") // Hello, Scala developer! val customGreeter = new CustomizableGreeter("How are you, ", "?") customGreeter.greet("Scala developer") // How are you, Scala developer? ``` +{% endscalafiddle %} Here, `DefaultGreeter` extends only a single trait, but it could extend multiple traits.