Chapter 1: Why Crystal?
You already write software in Ruby. It pays your bills. You enjoy it. Why should you care about Crystal?
Let’s think about Ruby for a minute: what’s its biggest weakness? For me, it’s these things:
    Concurrency
    Speed
    Documentation
What’s awesome about Ruby?
    Blocks
    Vaguely functional
    Syntax is pretty easy
    Focus on developer happiness
    Get up and running quickly
    Dynamically typed
So we could learn a lot from a language that’s easy as Ruby, handles concurrency well, and is fast. We don’t want to sacrifice anonymous functions, pretty syntax, or not making AbstractFactoryFactoryImpls just to get work done.
I think that language is Crystal.
Now: Crystal is not perfect. It is getting better. But the point is to learn. and using a language that’s very familiar, yet very different, can teach us a lot.
Here’s “Hello World” in Crystal:
1
puts "Hello, world!"
Copied!
Here’s a concurrent “Hello World” in Crystal:
1
channel = Channel(String).new
2
10.times do
3
spawn {
4
channel.send "Hello?"
5
}
6
puts channel.receive
7
end
Copied!
Here’s a rough port to Ruby:
1
10.times.map do
2
Thread.new do
3
puts "Hello?"
4
end
5
end.each(&:join)
Copied!
That’s it. Note the stuff that’s similar to Ruby:
    Pretty same syntax.
    Variables, while statically typed, have inference, so we don’t need to declare types
Here’s some stuff that’s different:
    Being compiled and statically typed the compiler will yell at us if we mess up.
Oh, and:
1
$ time ./hello
2
./hello 0.00s user 0.00s system 73% cpu 0.008 total
3
4
$ time ruby hello.rb
5
ruby hello.rb 0.03s user 0.01s system 94% cpu 0.038 total
Copied!
Five times faster. Yay irrelevant microbenchmarks!
Anyway, I hope you get my point: There’s lots of things about Crystal that make it syntactically vaguely similar enough to Ruby that you can feel at home. And its strengths are some of Ruby’s greatest weaknesses. That’s why I think you can learn a lot from playing with Crystal, even if you don’t do it as your day job.
Last modified 2yr ago
Copy link