LoginSignup
4
2

More than 5 years have passed since last update.

【Crystal】Crystal概要とHello WorldからHTTPサーバー

Posted at

Crystalについて

image.png
Ruby のように書きやすく C のように速いプログラミング言語
DMM INSIDEの記事

設計思想

  • Ruby 風な文法
  • 型推論による変数・メソッドの静的型付け
  • 容易に記述可能な C 言語バインディング
  • マクロとジェネリクスによるコンパイル時のコード生成
  • 高速なネイティブコードを出力

Crystal-JP

すごい
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
スクリーンショット 2018-05-31 2.53.21.png

思ったこと

  • Rubyを利用したことがある人はすぐに使えそう
  • Rubyが速度でディスられてるから、それに代わる言語になる??
  • 記事が少ない(Errorとか発生したら、IssueやPR読むほうが早く解決できるかも)
  • これからも勉強しようと思いました!

引用・参考

プログラミング言語 Crystal
DMM INSIDEの記事
Crystal-JP

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2