9
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

はじめに

elixir.jp Slackで、@sotashiro さん、 @zacky1972 先生が、Kernel.dbg/2をご紹介されていました。
とても便利そうです。
使ってみました。

2022/08/08現在、Elixir 1.14.0-rc.0で使用できます。

Kernel.dbg/2

論よりRun :rocket::rocket::rocket:
公式ドキュメントのサンプルです。

my_file.ex
"Elixir is cool!"
|> String.trim_trailing("!")
|> String.split()
|> List.first()
|> dbg()

my_file.exがあるとします。
早速Runしてみます。

$ elixir my_file.ex 

そうすると、こういった出力結果が得られます!

[my_file.ex:5: (file)]
"Elixir is cool!" #=> "Elixir is cool!"
|> String.trim_trailing("!") #=> "Elixir is cool"
|> String.split() #=> ["Elixir", "is", "cool"]
|> List.first() #=> "Elixir"

各行の実行結果が、#=> で表示されます。
以前は同じことをするのに、一行一行IO.inspect/2を入れて確かめたりしていました。
Kernel.dbg/2を使えばもうそんなことをする必要はないのです! 奥さん!

環境構築

以下、参考です。
asdfを使って私は、Elixir 1.14.0-rc.0をインストールしました。

asdfのインストールは以下の記事が詳しいです。

以下、asdfはインストール済みということで書いておきます。

$ mkdir dbg
$ cd dbg
$ asdf plugin update --all 
$ asdf list all erlang
$ asdf install erlang 25.0.3
$ asdf local erlang 25.0.3
$ asdf list all elixir
$ asdf install elixir 1.14.0-rc.0-otp-25
$ asdf local elixir 1.14.0-rc.0-otp-25
$ asdf reshim

asdf list all erlangasdf list all elixirでインストール可能なバージョンが表示されますので、実行されるときにお好みのバージョンをお使いください。
ここでは、2022/08/08現在においてKernel.dbg/2を利用できるバージョンで書いています。
ErlangはOTP-24でも大丈夫です。OTP-24を利用される場合は、Elixir1.14.0-rc.0-otp-24をインストールしてください。

おわりに

Kernel.dbg/2のご紹介をしました。
Kernel.dbg/2が入る前は同じことをしようとすると、一行一行IO.inspect/2を入れて確かめたりしていました。
Kernel.dbg/2を使うことで、便利にそしてさらに楽しくElixirのプログラミングを楽しめそうです :rocket::rocket::rocket:

Elixir 1.14のリリースが楽しみです :tada::tada::tada:

9
1
1

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
9
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?