Sascha Manns
Sascha Manns

Reputation: 2510

Getting a value from yaml

I have a yaml config file with that entries:

[...]
deploy:
- username: ext_username
- apikey: ext_apikey
[...]

Now i would like to read username and apikey and put them into two local variables. How can i do that?

Upvotes: 0

Views: 5340

Answers (1)

Drenmi
Drenmi

Reputation: 8777

You'll need to read the file and use the YAML module to parse its contents:

require "yaml"

config = YAML.load(File.read("path/to/config.yml"))

You can then access your configuration items from the parsed hash:

username = config["deploy"][0]["username"]
api_key = config["deploy"][1]["apikey"]

Note that you're making your deploy variable an array, which doesn't seem to be necessary in this case. Instead you could simplify it to:

deploy:
  username: ext_username
  apikey: ext_apikey

Accessing the values would then be done through:

username = config["deploy"]["username"]
api_key = config["deploy"]["apikey"]

Upvotes: 3

Related Questions