Reputation: 22366
In an Elixir program, I want to get the path of a certain directory from the environment variable V
, and as a fallback (if the variable is empty) use the working directory instead. I came up with the following solution, which looks overly complicated to me:
dirpath = System.get_env("V") || (File.cwd |> elem(1))
The reason for this construct is that File.cwd
returns a tuple, where the second element is the actual path, instead just the string of the working directory itself (which would be more natural to me).
Is my approach really the way an experienced Elixir programmer would do it? I'm coming from Ruby programming, and in Ruby I would simply write dirpath=ENV['V']||Dir.pwd
, but I guess I'm still thinking too much in the Ruby way when writing Elixir scripts.
Upvotes: 0
Views: 101
Reputation: 22366
I just found that there is also a function File.cwd!
, which returns the wd as a string. Hence I can do a
dirpath = System.get_env("V") || File.cwd!
Upvotes: 1