AndyL

AndyL

Can I extend iex helpers?

I’d like to add some custom helper functions to iex. I use .iex.exs and can define a module there with my custom functions. But I can’t figure out how to auto-import the module into my iex environment. I’ve tried hacking the iex bash script (at /usr/local/bin/iex), browsed thru the iex source - no luck. Is there a way to extend IEx.Helpers, or auto-import my .iex.exs module?

Most Liked

josevalim

josevalim

Creator of Elixir

In Elixir v1.14+ you will be able to import the module as you define it in .iex.exs files, as we now evaluate it line by line: Evaluate --dot-iex line by line · elixir-lang/elixir@395b053 · GitHub

AndyL

AndyL

I couldn’t figure out how to load .beam files with the Code module. I ended up putting my helper source and beam files in ~/.iex, then made a bash alias ie=iex -pa ~/.iex. The -pa bit adds the .iex directory to the load path.

axelson

axelson

Scenic Core Team

Is there an easy way to define an IEx helper now? I’d prefer to keep the helper definition inside .iex.exs if possible.

AndyL

AndyL

So Code.load_file didn’t work, but I found a solution:

  1. create a helper module (cd ~ && vim my_helpers.ex)
  2. compile the helper module (elixirc my_helpers.ex)
  3. add an import statement to .iex.exs (import MyHelpers)
  4. test in iex

What happens is that iex auto-loads the compiled beam file - then you can auto-import the helper module in .iex.exs.

You can tweak the beam-path by hacking the iex shell script (at /usr/local/bin/iex).

@gregvaughn thanks for your suggestions! :slight_smile:

Qqwy

Qqwy

TypeCheck Core Team

Unfortunately, no, because it is not possible to import a module in the same file as in which it was defined: you will get above compile error scenario.

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
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
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement