Hassle free Testing Elixir modules in iEx shell

Search for a command to run...

Very well written AbulAsar Sayyad
Thank you Mohd Shad Mirza :)
so in your example above both types of Calculator will be aliased as Calculator hence the only Calculator you will have access to thru Alias will be Logrithmic one.
Better to do
alias Mathematics.Calculator
alias Mathematics.Logrithmic.Calculator, as: LogCalc
Ah, thanks for pointing. I randomly added that example. I'll change it. Thanks
Wow! This a very well written article on how to use alias. Would you mind if I copy it or embed it in my blog?
Motivation Recently, I tried to learn some low-level system programming stuff. I am a Mac user, and I thought that everything that works on Linux should also work on Mac. After all, Mac is a Unix-based system 😊. I guess we all heard this. Oh boy! I ...

Problem Statement While working on a Nestjs project, I encountered a weird problem related to the database column. I was trying to insert a record into a MySQL table using TypeORM. The error I was experiencing stated that a specific column “cannot be...

Recently, I was exploring design patterns courses on my LinkedIn Learning subscription. I came across a course, Node.js: Design Patterns by Alex Banks. It is a wonderful, easy-to-understand course. I started with the Builder Pattern, and the explanat...

Suppose you are working on a table in a LiveView project. This table has limited static data of not more than one page (you can avoid questions about pagination in the comment section 😊). From a user's point of view, it becomes hard to look into the...

Recently, while working on one of my personal Elixir projects. I came across a scenario where I needed to make some changes to the database schema.The changes mainly revolve around adding and removing indexes due to changes in the business requiremen...

While working on Elixir project, we often test our functions or modules code in iex shell. Whatever change we make in our code, we fire iex -S mix command and all of our project modules are accessible in shell.
Suppose, we have a module say Calculator which is nested under project mathematics directory . As per Elixir naming convention we are going to name that module as Mathematics.Calculator. We also implemented some function say square in this module.
defmodule Mathematics.Calculator do
def square(number) do
number*number
end
end
For testing this square function we fire up iex shell. In Elixir, we have concept of alias using it we can avoid writing long module name. Using alias we can avoid Mathematics.Calculator, instead we can only use last word i.e Calculator. Refer example below.
> alias Mathematics.Calculator
> Calculator.square(2)
#=> 4
This looks fine but what if we add some more modules with deep level nesting modules say Mathematics.Logirithmic.LogCalculator. It becomes very cumbersome to alias these long named modules every-time we fire iex shell.
Create a file .iex.exs at root level of your Elixir project directory. Add all the frequently used or long named alias in the file.
alias Mathematics.Calculator
alias Mathematics.Logrithmic.LogCalculator
Next time when you fire up iex, you don't have to alias these modules again. It will be directly accessible in the shell.
Hope, you like this easy little hack. Let me know your thoughts.