1

With the 1.9 syntax ruby hashes and YAML are pretty close. I was wondering what ways there are of using a ruby file to retrieve some data.

Given that our config.rb is something like

{ api: "My key", name: "name" }

Option 1

Read and eval the file

config = eval open('./config.rb').read

Option 2

Require the file and then reference a variable

The config.rb file changes to the following (note CONFIG)

CONFIG = { api: "My key", name: "name" }

And then we can do

require './config'

# ...
config = CONFIG

Are there any other ways of doing this?

4

2 回答 2

0

Mark Ba​​tes 的配置器是我认为您正在寻找的一个很好的例子。查看他的代码,了解他如何使用 Ruby 通过 YAML 进行配置。

https://github.com/markbates/configatron

于 2013-04-16T17:38:33.297 回答
0

您不能执行选项 2。选项 1 很容易完成。但是使用yaml会更好。

于 2013-04-16T18:04:01.817 回答