From Ruby to Elixir: Stuck at the start (page 20)

I managed to install asdf, then erlang and elixir via asdf.

I was able to create and test the example Mix project (page 19) successfully.

I’m confused where to put the very first example: one.ex with two functions (hello/0 and welcome/1). The filenames given start with elixir_examples/lib so I assumed that ‘lib’ is the same lib directory created on page 19, and thus created the one.ex file in a new examples/modules directory. However, running iex as shown on page 21 generates a “function Examples.Modules.One.hello/0 is undefined (module Examples.Modules.One is not available)” error message. I am invoking $ iex -S mix from the same directory that contains lib and mix.exs.

I’ve played around for a couple hours and am stumped. Thanks in advance for a hint!

Can you paste your file that contains defmodule Examples.Modules.One ? The module should be in lib, and the file name does not matter. The only thing that matters is you define the module and functions in a file that’s inside of lib.

BTW, all of the books code is available on the website at From Ruby to Elixir: Unleash the Full Potential of Functional Programming by Stephen Bussey (Search “Source Code”). I highly recommend this if you get really stuck on something, because it may be a really simple missed command or misunderstanding somewhere.

The primary cause was a typo in the “defmodule” in one.ex.

However, I still find the path to the example files confusing: just prior to setting up the examples, the book has us run “mix new examples” (p19) which creates a new folder, “examples,” which contains a “lib” folder where we are to place our source code. The first example on the following page, however, places the “one.ex” in a path that has those folder names in reverse: “elixir_examples/lib/examples/modules/one.ex” so it’s confusing, especially since the top-level directory is different than the “examples” directory just created, and then we have to add two empty directories inside lib (including a confusing second instance of “examples”.

I think I understand it now, but I found it a bit confusing. Enjoying working through the rest of the otherwise excellent book, though!

Thank you Bruce.

You make fair points. The folder names in the example directory do not match yours exactly. This is a side effect of how I tried to keep Ruby and Elixir code obvious, but I probably should’ve made the paths match. (From the book bundler.)

It’s important to note that a file path inside of lib actually doesn’t matter. You could have one.ex at the top level and it would work. However, the most common pattern in the Elixir community is a subfolder for your project namespace, especially where each dot creates a new folder.