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
Like Julia, Crystal is another open-source language with a nice Wikipedia page, 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 API 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') nil end
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!' puts
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 editor. After that, we can hit run to see the output.
Alternatively, we can try to install the compiler 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:
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!
My content has recently grown popular enough to receive translations into different languages. I figured it was time to put together a collection of them.
The ACT/SAT discourse is back, and I found a pretty cool article debunking many of the common arguments for them.