Minimal Elixir Book Prep
send and receive messages
# A simple process that receives a message
pid = spawn(fn ->
receive do
{:hello, from} ->
IO.puts("Hello from #{inspect(from)}")
send(from, :ok)
end
end)
send(pid, {:hello, self()})
receive do
:ok -> IO.puts("All good!")
end
"Process ID : #{inspect(self())}"
send(self(), {:no_handler, "Sit idle"})
receive do
{:no_handler, msg} -> IO.puts(msg)
end
child_pid = spawn(fn ->
receive do
{:no_handler, msg} -> IO.puts(msg)
end
end)
send(child_pid, {:child, "are you OK?"})
IO.inspect(:erlang.process_info(self(), :messages), label: "Current mailbox")
spawn_link
spawn(fn -> raise "oops" end)
IO.puts("Still running!")
spawn_link(fn -> raise "fail fast" end)
IO.puts("you won't see this")
Process.flag(:trap_exit, true)
spawn_link(fn -> exit(:oops) end)
receive do
{:EXIT, pid, reason} ->
IO.puts("Linked process #{inspect(pid)} exited with reason: #{inspect(reason)}")
end
Regex
Regex.replace