Redoing this to be more clear:
Question
How can I call a function inside a module?
File: test.exs
defmodule MyModule do
def sum(a, b) do
a + b
end
MyModule.sum(1,1) # or sum(1,1)
end
elixir test.exs
:
** (UndefinedFunctionError) function MyModule.sum2/2 is undefined (function not available)
MyModule.sum2(1, 1)
==================================================
This section can be skipped
Original question asking same thing but more confused:
This is an .exs
file used for Phoenix migrations, so it’s inside the folder /priv/repo/migrations
. (In case that matters). I wrote a function but it’s totally blocked. I don’t want to import it from another module since this is where it goes (declared and used in same module)
defmodule MyApp.Repo.Migrations.MyModule do
use Ecto.Migration
def sum1(a, b) do #public
a + b
end
defp sum2(a, b) do #private
a + b
end
end
From iex -S mix
iex(2)> MyApp.Repo.Migrations.MyModule
MyApp.Repo.Migrations.MyModule
iex(3)> MyApp.Repo.Migrations.MyModule.sum2
** (UndefinedFunctionError) function MyApp.Repo.Migrations.MyModule.sum2/0 is undefined (module MyApp.Repo.Migrations.MyModule is not available)
MyApp.Repo.Migrations.MyModule.sum2()
iex(4)> MyApp.Repo.Migrations.MyModule.sum1(1,1)
** (UndefinedFunctionError) function MyApp.Repo.Migrations.MyModule.sum1/2 is undefined (module MyApp.Repo.Migrations.MyModule is not available)
MyApp.Repo.Migrations.MyModule.sum1(1, 1)
iex:4: (file)
If U can’t call it like this, this is what I want to do. It gives the same error as the above attempt.
defmodule MyApp.Repo.Migrations.MyModule do
use Ecto.Migration
def sum1(a, b) do
a + b
end
defp sum2(a, b) do
a + b
end
IO.puts(sum1(1,1)) #or sum2(1,1)
end
Pls Ignore it being bad practice (if it is) to have a function in a migration file, unless this the cause.
Real use case (Not sure if this link is accessible to all?)