gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:07:05 28 4
gpt4 key购买 nike

在 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com