Raymond Kroeker
Raymond Kroeker

Reputation: 532

Set Ruby runtime configuration parameters?

I'm looking to pass runtime parameters to my Ruby code. I have a Java background and, in the majority of cases, if I want to override a configuration I'd use a system property with a sane default value.

For example, if I wrote a test against a REST API to localhost, then want to run it against an integration environment and want to adjust the base URL:

$ rake -Cbaseurl=https://i-env.company.com/tbse/ test

Is there an equivalent to system properties in Ruby? Is there a standard pattern people are using?

I know how to use/load YAML configurations, as well as reference environment variables.

Upvotes: 1

Views: 859

Answers (1)

Linuxios
Linuxios

Reputation: 35803

Ruby's from the UNIX world, where this is command line arguments and environment variables.

Command line arguments should be used for things that are commonly changed and are run dependent. These are things like a verbose flag, an output file, or a target URL. Access these with the Ruby stdlib OptionParser.

Environment variables are for things that change less frequently, and are usually across applications. Things like system executable path ($PATH). Use the built-in Ruby env object to access these.

For other things, like configuration, use a config file. In Ruby, these are usually in the YAML format. Use the Ruby stdlib yaml library for these.

Upvotes: 1

Related Questions