ruby

Using environment variables in a file path


I want to use an input path in the following code:

File.exists?(File.expand_path(path))

Can I use environment variables in path, and what should the syntax be?


Solution

  • You can use standard ruby string interpolation (though it's a little bit wordy)

    path = "log/#{ENV['RAILS_ENV']}.log" # or whatever
    # >> "log/development.log"