DEV Community

Matías García Isaía for Crystal Language

Posted on • Originally published at crystal-lang.org

Hello World

This blogpost was originally posted by @asterite and @waj on the official Crystal Blog

This is the simplest way to write the Hello World program in Crystal:

puts "Hello World"

But if you feel like it, you can also use some object oriented programming:

class Greeter
  def initialize(@name : String )
  end

  def salute
    puts "Hello #{@name}!"
  end
end

g = Greeter.new("world")
g.salute

Or maybe with blocks:

"Hello world".each_char do |char|
  print char
end
print '\n'

Each alternative might have a different performance, but luckily all of them are pretty expressive.

Ok, but what's the purpose of learning a language if we cannot run the damn thing? Let's see how we do this with Crystal (and let's assume you already have it installed).

First create a file hello.cr containing your preferred choice of the previous examples.
Then type in the console:

$ bin/crystal hello.cr
$ ./hello
Hello World
$

The compiled output is a standalone executable without any specific runtime dependency. Neat! Isn't it?

Top comments (0)