Elixir

One often overlooked feature of using IEx to debug your code is to pre-load data into IEx using a .iex.exs file. In a nutshell, IEx will run the file before it completes loading and the execution is exactly like an other .exs scripts you may use.

For example, if we are working on a library that uses Ecto models to persist data, one way to debug the code would be to use the seeds.exs file to pre-load data into our development database. However, we can do the exact same thing with the .iex.exs file:

Let's say we have schema like this:

defmodule MyApp.User do
  use Ecto.Schema
  import Ecto.Changeset

  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id

  schema "users" do
    field :name, :string
    field :email, :string

    timestamps()
  end

  def changeset(%User{} = user, attrs) do
    organization
    |> cast(attrs, [:name, :email])
    |> validate_required([:name])
  end
end

We could include in our .iex.exs file:

users = [%MyApp.User{name: "Sam Smith"}, %User{name: "Jane Smith"}]

and have that pre-loaded as seed data when we invoke IEx:

$ iex -S mix
Erlang/OTP 19 [erts-8.2] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Interactive Elixir (1.4.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> users

[%MyApp.User{__meta__: #Ecto.Schema.Metadata<:built, "users">,
  email: nil, id: nil,
 inserted_at: nil, name: "Sam Smith", updated_at: nil}.
%MyApp.User{__meta__: #Ecto.Schema.Metadata<:built, "users">,
  email: nil, id: nil,
 inserted_at: nil, name: "Jane Smith", updated_at: nil}]

iex(2)>