In Elixir, how do you format numbers with string interpolation
You can use String.pad_leading/3 my_count |> Integer.to_string |> String.pad_leading(3, “0”)
You can use String.pad_leading/3 my_count |> Integer.to_string |> String.pad_leading(3, “0”)
This is most usually a sign that you haven’t imported appropriate macros from Ecto.Query.
To achieve this, you can prepend :add_ to the name before unquoting. Also, the parentheses after the method name in this case are required to prevent ambiguities. This should do the trick: defmacro generate_dynamic(name) do quote do def unquote(:”add_#{name}”)() do # … end end end
Kernel.length/1 will return the size of a list: length([1,2,3]) #3 You can do this from an Ecto query using: query = from d in Device, where: d.uuid == ^uuid, select: fragment(“count(?)”, d.id) assert Repo.all(query)== 3 In Ecto 2 you can use Repo.aggregate/4 query = from d in Device, where: d.uuid == ^uuid) assert Repo.aggregate(query, :count, … Read more
This: Notice that when the file does not exist, the version with ! raises an error. The version without ! is preferred when you want to handle different outcomes using pattern matching… will be more clear if you will look at the source code. The ! symbol in a function name is just a syntactic … Read more
Add to Map Use Map.put(map, key, value): map = Map.put(map, :d, 4) #=> %{a: 1, b: 2, c: 3, d: 4} Remove from Map Use Map.delete(map, key): map = Map.delete(map, :b) #=> %{a: 1, c: 3}
What you can do instead is to generate a Base64-encoded string to be used as a confirmation token. This confirmation token will then be saved to your DB and passed as params to the activation link. Your activation url would look something like: activation_url(MyApp.Endpoint, :confirm, confirm_id: confirm_id) The above url helper assumes you have a … Read more
For Erlang/OTP 20 This is built-in (from https://hexdocs.pm/iex/IEx.html#module-shell-history) From Erlang/OTP 20, it is possible to get shell history by passing some flags that enable it in the VM. This can be done on a per-need basis when starting IEx: iex –erl “-kernel shell_history enabled” If you would rather enable it on your system as a … Read more
A Little follow up on Patrick’s answer Using only create unique_index on your model will ultimately throw an exception instead of giving you an error. To get an error add a constraint on your changeset but as a paremeter you can give the index name created by unique_index. So in your migration file : create … Read more
Other answers already cover usage of raise vs. throw well. I’ll describe the mechanics of how to handle each exceptional situation using a table: creating | handling with | where y is —————————————————– raise x | rescue y | %RuntimeError{message: x} error(x) | rescue y | %ErlangError{original: x} throw x | catch y | x … Read more