The script I wrote runs at start up and requires that an environment variable be set, but which of Ruby's Exceptions, is best? I used LoadError
, I just want to be as descriptive as possible and follow the proper conventions.
Secondly, I can't find another way to see if an environment variable is set besides checking it's length, but that doesn't seem so elegant.
begin
raise LoadError if ENV['FOO'].to_s.length == 0
system "open http://example.com/" + ENV['FOO']
rescue Exception => e
puts "=> #{e} FOO environment variable not set"
end
An environment variable is a key/value pair, it looks like this: We use these variables to share configuration options between all the programs in your computer. That’s why it’s important to learn how they work & how to access them from your Ruby programs using the ENV special variable.
The program stops if an exception occurs. So exceptions are used to handle various type of errors, which may occur during a program execution and take appropriate action instead of halting program completely. Ruby provide a nice mechanism to handle exceptions.
Closed environment, changing variables inside a process (your Ruby program is a process) doesn't change the environment variables outside the process
Then click on Advanced System Setting option then under Advanced tab click on Environment Variables. Now, we have to Edit the “Path” variable under System variables so that it also contains the path to the Ruby environment.
You can do something like:
ENV['SECRET_KEY_XXYY'] || raise('no SECRET_KEY_XXYY provided')
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