はじめに
- 2020/10/17(土)に行われた【オンライン】kokura.ex#14:Elixirもくもく会~入門もあるよ(9:30~)祝1周年!に参加して書きました
- @im_miolab さん、ありがとうございます!
- 2019/01/25(金)に行われたkokura.ex#1:小倉Elixirコミュニティ発足【セッション/LTと懇親会】に参加してから私はElixirと出会いました
- 当日は
brew install elixir
でインストールしたElixirでIExでHello, world
だけ行った経験だけでしたが、思い切ってLTをした思い出があります
- 標題の件で何度もあれれ? となったのでいい加減同じことで詰まらないように記事にしておきます
インストール
mix new
- 適当にプロジェクトを作ります
$ mix new hello_env
* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating lib
* creating lib/hello_env.ex
* creating test
* creating test/test_helper.exs
* creating test/hello_env_test.exs
Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:
cd hello_env
mix test
Run "mix help" for more commands.
System.get_env/2で環境変数を読み出してModule attributeに格納しておくモジュールを作ります
lib/hello_env.ex
defmodule HelloEnv do
@greet System.get_env("AWESOME_ENVIRONMENT_VARIABLE")
def greet do
@greet
end
end
環境変数を設定します
- 私はAppleに言われるままにzshを使っています
- お使いの環境にあわせていい感じのファイルに書いてください(~/.profileや~/.bash_profile等)
.zshenv
export AWESOME_ENVIRONMENT_VARIABLE="I was born to love Elixir."
実行します
$ source ~/.zshenv
$ cd hello_env
$ iex -S mix
Erlang/OTP 23 [erts-11.0.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]
Compiling 1 file (.ex)
Generated hello_env app
Interactive Elixir (1.10.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> HelloEnv.greet
"I was born to love Elixir."
- いいね
AWESOME_ENVIRONMENT_VARIABLE
を変えたくなりました
- IExを終わらせます
iex(2)> System.halt
- 環境変数を変更します
.zshenv
export AWESOME_ENVIRONMENT_VARIABLE="I was born to love Elixir. We are the Alchemists, my friends."
$ source ~/.zshenv
$ iex -S mix
Erlang/OTP 23 [erts-11.0.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]
Interactive Elixir (1.10.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> HelloEnv.greet
"I was born to love Elixir."
- あれ!?、変更内容が反映されていないなあ
解決法①
iex(2)> recompile force: true
Compiling 1 file (.ex)
Generated hello_env app
:ok
iex(3)> HelloEnv.greet
"I was born to love Elixir. We are the Alchemists, my friends.
解決法②
$ source ~/.zshenv
$ mix clean
$ iex -S mix
Erlang/OTP 23 [erts-11.0.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]
Compiling 1 file (.ex)
Generated hello_env app
Interactive Elixir (1.10.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> HelloEnv.greet
"I was born to love Elixir. We are the Alchemists, my friends."
解決法③(ダサいけどいつも私がやっていたこと)
-
lib/hello_env.ex
がコンパイルエラーを起こすように変なことをしておく
iex -S mix
Erlang/OTP 23 [erts-11.0.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]
Compiling 1 file (.ex)
== Compilation error in file lib/hello_env.ex ==
** (SyntaxError) lib/hello_env.ex:24: unexpected token: end
(elixir 1.10.4) lib/kernel/parallel_compiler.ex:304: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7
- コンパイルが通るようにする
iex -S mix
Erlang/OTP 23 [erts-11.0.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]
Compiling 1 file (.ex)
Interactive Elixir (1.10.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> HelloEnv.greet
"I was born to love Elixir. We are the Alchemists, my friends."
Wrapping Up
- Enjoy Elixir !!!