まずは、本の説明に出てきた Time モジュールに triple 関数を追加。
defmodule Times do
def doubles(n) do
n * 2
end
def triple(n) do
n * 3
end
end
そしてこれを2つのやり方で実行。1つは iex の引数として読み込んで実行する。
^o^ > iex practice_6_1.exs Eshell V8.0 (abort with ^G) Interactive Elixir (1.3.4) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> Times.triple(2) 6
もう1つのやり方は、iex の中で c コマンドを使ってファイルをコンパイルして読み込む。
^o^ > iex Eshell V8.0 (abort with ^G) Interactive Elixir (1.3.4) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> c "practice_6_1.exs" [Times] iex(2)> Times.triple(2) 6
こっちのやり方だと、モジュールをコンパイルした Elixir.Times.beam ファイルができるんだな。
最後に、Time モジュールに quadruple 関数を追加。
defmodule Times do
def doubles(n) do
n * 2
end
def triple(n) do
n * 3
end
def quadruple(n) do
doubles(n) * doubles(n)
end
end
実行してみる。
^o^ > iex practice_6_3.exs Eshell V8.0 (abort with ^G) Interactive Elixir (1.3.4) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> Times.quadruple(2) 16