Crystalについて
Ruby のように書きやすく C のように速いプログラミング言語
DMM INSIDEの記事
設計思想
- Ruby 風な文法
- 型推論による変数・メソッドの静的型付け
- 容易に記述可能な C 言語バインディング
- マクロとジェネリクスによるコンパイル時のコード生成
- 高速なネイティブコードを出力
すごい
C言語の数十倍から数百倍遅くなると言われまくってるRubyに対して、ほぼ同じような書き方でCやC++,Rustに匹敵する高いパフォーマンス!!らしい!!
少し使って見た
CrystalによるHello World!
- Crystal : 0.24.2 (2018-03-10)で実行した
Crystalはプログラム自体がメインルーチンなので
helloworld.cr
puts "Hello world"
これだけで動く!しかも、class
def
module
include
などRubyのシンタックスと同様の書き方らしい!!マジでRubyじゃん!!GOD!!
実行コマンド
$ crystal helloworld.cr
実行ファイルの作成
$ crystal build helloworld.cr
$ ./helloworld
続けてHTTPサーバーを作成してみる
CrystalによるHTTPサーバー
HTTPサーバーを作成する。
http_server.cr
require "http/server"
server = HTTP::Server.new(8080) do |context|
context.response.content_type = "text/plain"
context.response.print "Hello world! The time is #{Time.now}"
end
puts "Listening on http://0.0.0.0:8080"
server.listen
これを実行すると以下のErrorが
Package libssl was not found in the pkg-config search path.
Perhaps you should add the directory containing `libssl.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libssl' found
Package libcrypto was not found in the pkg-config search path.
Perhaps you should add the directory containing `libcrypto.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libcrypto' found
解決策
Crystal-langのIssueに解決策がありました。
$ brew intsall openssl
.zshrc
または、.bashrc
に以下を記述
export PKG_CONFIG_PATH=/usr/local/opt/openssl/lib/pkgconfig
http://0.0.0.0:8080/ にアクセスして以下のような表示がでればLGTM
思ったこと
- Rubyを利用したことがある人はすぐに使えそう
- Rubyが速度でディスられてるから、それに代わる言語になる??
- 記事が少ない(Errorとか発生したら、IssueやPR読むほうが早く解決できるかも)
- これからも勉強しようと思いました!