Crystal Playground is developed together with Crystal Compiler.
You can find the source code at https://github.com/crystal-lang/crystal/tree/master/src/compiler/crystal/tools/playground.
Currently you are using .
Start typing code in the editor. When you stop typing or click the green button the code will be compiled and executed. Go ahead, try it!
a = 1 b = 3
As you can see, some values appeared on the side. These are the values that each line generates.
When a program prints, like when using puts
, the output is visible in the bottom panel.
s = "Lorem ipsum" puts s
In the main playground this is hidden by default and will appear when is pressed. Since the sidebar will likely show all the needed information you won't need this output all the time. Also, puts
returns nil
, but the sidebar will show the argument instead of the returned value. The same applies for print
and raises
.
Loops, functions, classes, sleeps and the whole language is available. When a line is executed multiple times, all produced values are collected and can be inspected in a table.
def sample(n) res = [] of Float64 n.times do res << rand end res end sample 5
And you can use the whole standard library. Seriously. Run the next sample and click here . Once you finish you can stop the long running process with the stop button. That will kill the process, hence display an exit status and a .
require "http/server" server = HTTP::Server.new do |context| context.request.path context.response.headers["Content-Type"] = "text/plain" context.response.print("Hello world!") end server.bind_tcp "0.0.0.0", 5678 puts "Listening on http://0.0.0.0:5678" server.listen
When crystal play
is run from your awesome Crystal app or shard you are able to use require
as you would normally do: require relative files and even dependencies declared in your shards.yml
.
In loops it is sometimes useful to inspect the values of multiple variables. Tuple literals are handy for this since each element of the tuple will appear in a different column in the table.
(1..100).each do |i| r = case {i % 3 == 0, i % 5 == 0} when {true, true} "FizzBuzz" when {true, _} "Fizz" when {_, true} "Buzz" else i end {i, r, i % 3, i % 5} end
And regarding errors, try to fix the following code one step at a time. You can see the full trace when clicking the inline errors.
def bar "Hurray " + 42 end baz
In the playground your code is persisted in every stroke and before you close or refresh the window. It is persisted in both SessionStore
and LocalStore
if available. This means that next time you open the playground the code will still be there!
You are able to save the current code as a file and even share it as a gist from the action buttons in the bottom right corner.
For loading a Crystal .cr
file, just drag and drop it anywhere in the playground. Or start the server with $ crystal play ./path/to/file.cr
.
In the workbook you will find a listing of all the files in the ./playground
folder of your current location.
These files can be .md
, .html
and even .cr
. And will be displayed with playground editor.
The following libraries are used in Crystal Playground.
Thanks for all the hard work!