Reusing code in elixir scripts (.exs)

Curious to learn strategies to reuse code in elixir scripts. I would like to use elixir for scripting things (inspired by underjord), but I feel blocked in being able to write reusable code that I can import into these scripts.

I’m trying to do something like this:

a.exs

Mix.install([
  {:hello, path: "./b"}
])

defmodule ATest do
  def a_test() do
    Hello.world()
  end
end

ATest.a_test()

b.exs

defmodule Hello do
  use Mix.Project


  def world() do
    "hello_world"
  end
end

You can use Code.require_file.

Welcome to Elixir Forum!

EDIT: Mix.install with a path should work but it would need to be a proper mix project with a mix.exs file with at very least a def project and a lib/ directory.

1 Like

These may be of interest:

2 Likes

Does it have to be a file, or does Elixir have a way of loading and running from a url?

I was using Deno, and really like being able to run something just like in a web browser, point to a url and it runs. I guess there’s all this stuff about how it’s kind of tied to this security model stuff there… but I just like it for the convenience.

It’s one more line of code, jist fetch the script with Req and then load it.