Book Image

Crystal Programming

By : George Dietrich, Guilherme Bernal
Book Image

Crystal Programming

By: George Dietrich, Guilherme Bernal

Overview of this book

Crystal is a programming language with a concise and user-friendly syntax, along with a seamless system and a performant core, reaching C-like speed. This book will help you gain a deep understanding of the fundamental concepts of Crystal and show you how to apply them to create various types of applications. This book comes packed with step-by-step explanations of essential concepts and practical examples. You'll learn how to use Crystal’s features to create complex and organized projects relying on OOP and its most common design patterns. As you progress, you'll gain a solid understanding of both the basic and advanced features of Crystal. This will enable you to build any application, including command-line interface (CLI) programs and web applications using IOs, concurrency and C bindings, HTTP servers, and the JSON API. By the end of this programming book, you’ll be equipped with the skills you need to use Crystal programming for building and understanding any application you come across.
Table of Contents (26 chapters)
1
Part 1: Getting Started
5
Part 2: Learning by Doing – CLI
10
Part 3: Learn by Doing – Web Application
13
Part 4: Metaprogramming
18
Part 5: Supporting Tools

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:

  1. Save this on a file called hello.cr.
  2. Run it with crystal run hello.cr on your terminal. Note the output.
  3. 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:

  1. Save the above code on a file called "hello_name.cr".
  2. Run it with crystal run hello_name.cr on your terminal.
  3. It will ask you for your name; type it and press Enter.
  4. 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.