I've in Ruby following expression:
env = opts.env || "staging"
How to write it in Elixir?
EDIT:
This expression in Elixir won't work:
case Repo.insert(changeset) do
{:ok, opts} ->
env = opts.env || "staging"
Error:
** (KeyError) key :env not found in: %Myapp.App{__meta__: #Ecto.Schema.Metadata<:loaded>
You can set the default values for variables by adding ! default flag to the end of the variable value. It will not re-assign the value, if it is already assigned to the variable.
undefined is a type by itself (undefined). Unassigned variables are initialized by JavaScript with a default value of undefined. Here as the variable is declared but not assigned to any value, the variable by default is assigned a value of undefined. On the other hand, null is an object.
The exact same idiom works (assuming by "failed" you mean opts.env is nil):
iex(1)> nil || "staging"
"staging"
iex(2)> "production" || "staging"
"production"
Elixir, as Ruby, treats nil as a falsy value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With