Skip to content

Instantly share code, notes, and snippets.

@philtr
Last active December 24, 2015 07:49
Show Gist options
  • Save philtr/6766102 to your computer and use it in GitHub Desktop.
Save philtr/6766102 to your computer and use it in GitHub Desktop.
Environment variable Ruby interface.
module Environment
class Grabber
include Environment
end
module_function
def get(key)
ENV[key.to_s.upcase]
end
def method_missing(method, *args)
environment_var = get(method)
super(method, *args) if environment_var.to_s.empty?
get(method)
end
end
# Traditional
ENV["SOME_ENV_VAR"] # => "test test 123"
# Module Function
Environment.some_env_var # => "test test 123"
# Instance
@env = Environment::Grabber.new
@env.some_env_var # => "test test 123"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment