Язык программирования

Hello World

10 Jul 2013 by asterite, waj

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)
    @name = name.capitalize
  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?

comments powered by Disqus