Hello World in Crystal

Hello World in Crystal Featured Image

Hello and welcome to the 26th edition of Hello World in Every Language. Lately, we’ve been poking away at newer open-source languages. For instance, we just cover Julia, a numerical analysis language. Today, we’re jumping back on the general-purpose parade with Hello World in Crystal, a language that launched in 2014.

Table of Contents

Crystal Background

Like Julia, Crystal is another open-source language with a nice Wikipedia pageOpens in a new tab., so we’ll be using that to get some language background.

According to Wikipedia, Crystal is a general-purpose language that first appeared in 2014. As of today, Crystal is self-hosted meaning the compiler is written in an earlier version of Crystal.

That said, Crystal was inspired by Ruby which is the language Crystal was originally written in. However, as previously mentioned, Crystal is a compiled language, so many of the dynamic features of Ruby no longer exist.

Despite the lack of dynamic features, Crystal leverages strong type inference to allow for implicit typing. As a result, the language looks like a high-level scripting language. However, the language’s efficiency is much closer to lower-level languages like C.

Hello World in Crystal

With the background out of the way, let’s implement Hello World in Crystal:

puts "Hello, World!"

If we think back, we might remember that this syntax is exactly the same in Ruby. Of course, this should come as no surprise as Ruby’s syntax was a major influence on Crystal.

Digging through the APIOpens in a new tab. reveals that there are four definitions of puts:

def puts(*objects : _) : Nil
def puts : Nil
def puts(obj) : Nil
def puts(string : String) : Nil

In our case, we’re using option four which simply writes a string to standard output.

Now, puts is pretty interesting because it automatically appends a new line unless a new line already exists. Personally, that’s the first time I’ve heard of a library call doing that kind of string formatting for the user. So, my question becomes: what happens if the string ends with multiple new lines?

Based on the source code:

def puts(string : String) : Nil
    self << string
    puts unless string.ends_with?('\n')

The puts library appears to only remove the last new line. After running it, I can confirm that’s all this function does. Now, that’s some bizarre behavior:

puts 'Hello, World!'  # Writes 'Hello, World!\n'
puts 'Hello, World!\n'  # Writes 'Hello, World!\n'
puts 'Hello, World!\n\n'  # Writes 'Hello, World!\n\n'

Honestly, I find this a little buggy. If, by default, this function adds a new line, then I would instinctively add a new line to the string (see line 2 above) to create extra space.

Of course, that doesn’t work. I suspect that Crystal style would prefer the following:

puts 'Hello, World!'

Or, something along those lines. At any rate, I’ve gone a bit too far down a rabbit hole. Let’s learn how to run our solution.

How to Run the Solution

If we want to run our solution, perhaps the easiest thing to do is to copy our solution into the online Crystal editorOpens in a new tab.. After that, we can hit run to see the output.

Alternatively, we can try to install the compilerOpens in a new tab. on our system. However, I won’t bother going into that because I’m using a Windows PC which doesn’t appear to be supported.

Sample Programs in Every Language

Once again, thanks for stopping by. Up next, I’ll be looking to play with some of the following languages:

  • Ring
  • Pyret
  • Lily
  • Groovy
  • Tcl

If you enjoyed this article, please give it a share. It helps the series grow, and I’m always looking for people to fork my repo.

Of course, if you have any recommendations for future languages, let me know in the comments. Until next time!

Sample Programs in Every Language (44 Articles)—Series Navigation

For 100 Days of Code, I’ve decided to implement a few sample programs in as many languages as possible. Each implementation details a brief history of the language and a description of the code.

The plan for the series is to explore the major general-purpose language like Java, Python, C, C++, and C#. From there, we’ll take a look at some sample programs in web development languages like Ruby, PHP, and JavaScript. As we continue, we’ll cover proprietary languages like Swift and Objective-C. Eventually, we’ll start to tackle less popular languages like Rust, x86, and Verilog. Finally, we’ll play around with some of the esoteric languages like Brainf*ck and LOLCODE.

Who knows? Maybe the Sample Programs in Every Language series will become so popular it’ll never end. To help this series grow, consider sharing it on social media with your friends. Or, if you have a language you want to see, drop your suggestion in the comments.

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Posts