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?

Uri
  • 2,306
  • 2
  • 24
  • 25
  • your question is not clear. – Arup Rakshit Apr 16 '13 at 17:29
  • How about eval (http://stackoverflow.com/a/7730183/337). Not saying it's a great idea, but it's "another way". – Larsenal Apr 16 '13 at 17:30
  • possible duplicate of [ruby: how to load .rb file in the local context](http://stackoverflow.com/questions/4048093/ruby-how-to-load-rb-file-in-the-local-context) – user229044 Apr 16 '13 at 17:43
  • There are some similarities, but a lot more things aren't the same. Keep your data as YAML when it's on disk, or use Marshall or JSON, but don't trust trying to parse a `to_s` or `inspect` version of the Ruby object. Also, YAML and JSON are portable so you can read/write the data from other languages. That's important as your system grows. – the Tin Man Apr 16 '13 at 19:35

2 Answers2

0

Mark Bates's configatron is a good example of what I think you are looking for. Check out his code and see how he uses Ruby for configuration over YAML.

https://github.com/markbates/configatron

Brian Knight
  • 4,970
  • 28
  • 34
0

You cannot do option 2. Option 1 is easily done. But using yaml would be better.

sawa
  • 165,429
  • 45
  • 277
  • 381