elixir - 是否可以启动交互式 elixir shell 并立即调用函数?

标签 elixir elixir-iex

在 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 时不会.

关于elixir - 是否可以启动交互式 elixir shell 并立即调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55559141/

相关文章:

monads - 如何在 Elixir 中编写一元值?

elixir - IEx - 如何取消多行命令?

elixir - 如何在现有表 Phoenix 中添加字段

elixir - 如何对 Elixir REPL、iex 进行垃圾回收?

elixir - 增加 Elixir IEx.pry session 的超时时间

Elixir:如何处理最后没有的默认参数?

import - Elixir:要求和导入之间的区别

elixir - 来自 elixir 脚本的非常基本的 IEx 和 IEx.pry 得到错误 : IEx shell running?

python-3.x - 连接到 "Iex"或 "morningstar"并检索数据