是否可以启动交互式长生不老药 shell 并立即调用函数?

Is it possible to launch an interactive elixir shell and call a function immediately?

在 elixir 中,您可以通过以下命令将 iex shell 与您的应用程序上下文一起使用...

iex -S mix

这个命令非常方便,让我们可以在交互中使用应用程序功能 shell。

我想知道是否可以使用参数启动交互式 shell,或者是否可以让您的应用程序在用户与 shell 交互之前调用一些函数。

例如,假设我有一个名为 MyApp 的应用程序,我以 iex -S mix 启动它。无需手动将命令输入 shell,例如

iex()> import MyApp.Module

iex()> alias MyApp.Module.OtheName

iex()> value = OtherName.function(1234)

有一种方法可以自动发生这种情况。

您可以在您的应用程序目录中创建一个文件 .iex.exs,启动时代码为 运行。来自 iex 的手册页:

After iex starts, it seeks the file .iex.exs and, in a case of success, executes the code from the file in the context of the shell. At first the search starts in the current working directory, then, if necessary, it continues in the home directory.

但是请注意,如果您将特定于应用程序的代码放入此文件中,则只有在您执行 iex -S mix 时它才会 运行 正确,但如果您只执行 iex.