chasen-bettinger

chasen-bettinger

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

Most Liked

AndyL

AndyL

These may be of interest:

sodapopcan

sodapopcan

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.

Last Post!

dimitarvp

dimitarvp

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

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New

Other popular topics Top

dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

We're in Beta

About us Mission Statement