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?
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"
path.To expand environment variables, you should do it yourself:
def expand_env(str)
str.gsub(/\$([a-zA-Z_][a-zA-Z0-9_]*)|\${\g<1>}|%\g<1>%/) { ENV[$1] }
end
expand_env("${SHELL}:%USER%:$PAGER")
# => "/bin/bash:amadan:less"
(both Windows-style and Unix-style are supported, but only the basic substitution, and not any crazy stuff that bash is capable of).
pathto be%MY_VAR%/bin. This syntax doesn't work, and neither does$MY_VAR/bin. Is there a different syntax that should work?ENV['MY_VAR'], Is this what you mean?