Over a million developers have joined DZone.

Understanding Command Line Logistics in Crystal, the Newest OO Language

DZone 's Guide to

Understanding Command Line Logistics in Crystal, the Newest OO Language

Let's dive into how Crystal's command line works and what separates it from Ruby's.

· Open Source Zone ·
Free Resource

Crystal is a general-purpose object-oriented programming language. Its syntax was inspired by Ruby, but there is little compatibility between the two languages. Crystal was developed because many programmers appreciated both the efficiency of writing Ruby code and the efficiency of executing C code. It is the best of both worlds.

I know many developers have started expressing an interest in Crystal. However, grasping the basics can be a little challenging at first. You need to understand how to use the command lines in Crystal to start building code.

Understanding the Differences Between Ruby and Crystal Before You Start Using Command Lines

Command lines in Crystal are a bit similar to Ruby. However, you need to appreciate the differences before you can start using them.

There are two fundamental differences between Ruby and Crystal. These differences include the following:

  • Crystal is a compiled language. The code is converted to native machine code through LLVM. Ruby, on the other hand, is an interpreted language.
  • Crystal has a static typing system, so type checking is done during compilation. This adds an extra layer of security to the code, which helps prevent type errors. Ruby is dynamically typed, which means that types are checked in execution. However, the Crystal compiler has a type inference system, so it is not necessary to declare the types of all values and executions since the compiler can detect them. In this way, the code is kept clean. This is a core component of the philosophy of this programming language.
  • You may need to use an Android emulator if you plan on writing code for Android devices.

The following code shows us the inference of types in action:

puts typeof("Michelle") #=> String
puts typeof(155) #=> Int32
puts typeof(37.1) #=> Float64

We can also define the type with annotations:

Subject name: String

Subject age in years: Int32

As in Ruby, every element of code in Crystal is contained in an object. It has a type and it responds to certain methods.

Crystal is still a new language, but it is growing and changing fast. Some ambitious pioneers in the programming community have begun using Crystal in production. One major project has a Roadmap that defines the things they want to have in the language, as well as frequently asked questions and excellent documentation.

There are other concepts in Crystal that are beyond the scope of this post, such as union types (special data types that can have several unique representations), Macros (systems for meta programming), the concurrency model and integration with C libraries (it has a dedicated syntax to call native libraries easily).

Installing Crystal From the Command Line in Your Operating System

Crystal can be installed on RedHat, Linux and many other operating systems. It can be installed from a tar.gz or compiled from the source code. If you use the asdf version manager, there is a plugin for it. We can see the different installation instructions in the documentation.

You need to use the Crystal command to get started. Once installed, we should have the crystal command available in our terminal. Crystal source code files have the .cr extension. To run a Crystal source code file, you must write the following:

crystal run file.cr

The command `run` compiles the code to an executable binary in a temporary location and executes it. With the crystal build command, we can go to the command line and create an executable binary with this syntax:

Crystal build

There is also crystal eval to pass Crystal code to crystal run from the command line or through a pipe through the standard input. The complete list of options is in the documentation using the compiler.

Creating Crystal projects

The crystal command also includes the init parameter, which initializes a directory with a project. The first init parameter is the project type. The two options are a lib, a reusable library and app, an application without the purpose of being used as a dependency:

Shards have the same role in Crystal that gems have in Ruby. Crystal applications and libraries have a shard.yml file that declares the dependencies of the project. Shards functionality is distributed with Crystal, so we can access it from our terminal. With shard init, we create an example shard.yml file, followed by shard install to install the dependencies and specify them in a shard.lock file.

Cossack is a fairly simple HTTP client for Crystal. It is installed by running shards install and I can already use it in the source code. Inside the src/test.cr file of the same project I write:

Testing Crystal Code

Crystal comes equipped with a testing library in the Spec module. It includes a DSL (specific domain language) inspired by RSpec, making it super familiar.

command line ,open source ,crystal ,oo language ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}