Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Crystal Programming

You're reading from   Crystal Programming A project-based introduction to building efficient, safe, and readable web and CLI applications

Arrow left icon
Product type Paperback
Published in May 2022
Publisher Packt
ISBN-13 9781801818674
Length 356 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
George Dietrich George Dietrich
Author Profile Icon George Dietrich
George Dietrich
Guilherme Bernal Guilherme Bernal
Author Profile Icon Guilherme Bernal
Guilherme Bernal
Arrow right icon
View More author details
Toc

Table of Contents (26) Chapters Close

Preface 1. Part 1: Getting Started
2. Chapter 1: An Introduction to Crystal FREE CHAPTER 3. Chapter 2: Basic Semantics and Features of Crystal 4. Chapter 3: Object-Oriented Programming 5. Part 2: Learning by Doing – CLI
6. Chapter 4: Exploring Crystal via Writing a Command-Line Interface 7. Chapter 5: Input/Output Operations 8. Chapter 6: Concurrency 9. Chapter 7: C Interoperability 10. Part 3: Learn by Doing – Web Application
11. Chapter 8: Using External Libraries 12. Chapter 9: Creating a Web Application with Athena 13. Part 4: Metaprogramming
14. Chapter 10: Working with Macros 15. Chapter 11: Introducing Annotations 16. Chapter 12: Leveraging Compile-Time Type Introspection 17. Chapter 13: Advanced Macro Usages 18. Part 5: Supporting Tools
19. Chapter 14: Testing 20. Chapter 15: Documenting Code 21. Chapter 16: Deploying Code 22. Chapter 17: Automation 23. Other Books You May Enjoy Appendix A: Tooling Setup 1. Appendix B: The Future of Crystal

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.

You have been reading a chapter from
Crystal Programming
Published in: May 2022
Publisher: Packt
ISBN-13: 9781801818674
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image