DEV Community

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

Posted on • Originally published at crystal-lang.org

1

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?

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up