Creating our first program
Now let's experiment with creating our first program using Crystal. This is the basis for how you will write and execute code for the remainder of this book. Here is our first example:
who = "World" puts "Hello, " + who + "!"
After that, perform the following steps:
- Save this on a file called
hello.cr
. - Run it with
crystal run hello.cr
on your terminal. Note the output. - Try changing the
who
variable to something else and running again.
There is no boilerplate code such as creating a static class or a "main" function. There is also no need to import anything from the standard library for this basic example. Instead, you can just start coding right away! This is good for quick scripting but also makes applications simpler.
Note that the who
variable doesn't need to be declared, defined, or have an explicit type. This is all deduced for you.
Calling a method in Crystal doesn't require parentheses. You can see puts
there; it's just a method call and could have been written as puts("Hello, " + who + "!")
.
String concatenation can be done with the + operator. It's just a method defined on strings, and you'll learn how to define your own in later chapters.
Let's try something else, by reading a name inputted by the user:
def get_name print "What's your name? " read_line end puts "Hello, " + get_name + "!"
After that, we'll do this:
- Save the above code on a file called "
hello_name.cr
". - Run it with
crystal run hello_name.cr
on your terminal. - It will ask you for your name; type it and press Enter.
- Now, run it again and type a different name. Note the output changing.
In this example, you created a get_name
method that interacts with the user to obtain a name. This method calls two other methods, print
and read_line
. Note that as calling a method doesn't require parentheses, a method call without arguments looks precisely like a variable. That's fine. Also, a method always returns its last expression. In this case, the result of get_name
is the result of read_line
.
This is still simple, but will get you started on writing more complex code later on. Here, you can already see some console interaction and the use of methods for code reusability. Next let's see how you can make a native executable out of this code.
Creating an executable
When you need to ship your application, either to your end user's computer or to a production server, it isn't ideal to send the source code directly. Instead a better approach is to compile the code down to a native binary executable. Those are more performant, hard to reverse-engineer, and simpler to use.
So far, you have been using crystal run hello.cr
to execute your programs. But Crystal has a compiler, and it should also produce native executables. This is possible with another command; try crystal build hello.cr
.
As you will see, this won't run your code. Instead, it will create a "hello
" file (without an extension), which is a truly native executable for your computer. You can run this executable with ./hello
.
In fact, crystal run hello.cr
works mostly as a shorthand for crystal build hello.cr && ./hello
.
You can also use crystal build --release hello.cr
to produce an optimized executable. This will take longer, but will apply several code transformations to make your program run faster. For more details on how to deploy a final version of your application, take a look at Appendix B, The Future of Crystal.