load
overcomes this, but it doesn't do the "read this only once", which require
does implicitly.
As an alternative, you could create a ruby script called "load_config.rb" which consists of only:
load '~/.config'
then use:
require 'load_config'
Some advantages of this solution over simply using load 'config'
:
- load_config.rb could load a global configuration and then the user configuration, so this logic doesn't have to be spread across all your code
- it's easy to change the location of the config file (you change it only in load_config.rb, rather than in all the scripts that need this configuration information)
If you want to share config settings between Ruby, Perl, or any other set of languages, I really don't think you want to use hackish features like coincidental similarities in syntax. Obviously, this limits not only the types of configuration data (and more significantly, scoping!) you can use, but the set of languages it will work in.
You could probably find a handful of examples that would be compatible between Ruby and Perl, Ruby and Python, Perl and C, etc., etc., but no two syntax intersections would be alike, and all would be extremely brittle and sensitive to minor quirks/idioms in each of the languages used.
Really, if you want a multi-language configuration solution, it's much better to use a portable data format like YAML or XML, which you can be relatively sure is parseable in just about any language, (and on any platform).
Okay, forget the config example. Let's say you have a cgi-script that you run in mod_ruby:
wiki.rbx
This script also exposes a library of Wiki functions, so when you require the file you get that api:
require 'wiki.rbx'
Why should it have to be named
wiki.rb
for ruby to require it?
I think that it's reasonable:
- only .rb and .so are guessed; ie. require 'wiki' looks for: wiki, wiki.rb, wiki.so in some decreed order.
- anything besides .so is a text file. I accept that ruby needs to know that .so files are a binary format.
I don't really see any added value with the rule that only .rb files can be required. I can see value to allowing ruby to require any text file (that doesn't end in .so)
load does some of require.... (HughSasse, 2003-07-25 10:46:07)
load
overcomes this, but it doesn't do the "read this only once", whichrequire
does implicitly.Re: load does some of require.... (, 2003-07-27 01:07:18)
As an alternative, you could create a ruby script called "load_config.rb" which consists of only:
then use:
Some advantages of this solution over simply using
load 'config'
:Sharing configuration (lennon, 2003-07-29 07:27:50)
You could probably find a handful of examples that would be compatible between Ruby and Perl, Ruby and Python, Perl and C, etc., etc., but no two syntax intersections would be alike, and all would be extremely brittle and sensitive to minor quirks/idioms in each of the languages used.
Really, if you want a multi-language configuration solution, it's much better to use a portable data format like YAML or XML, which you can be relatively sure is parseable in just about any language, (and on any platform).
forget config; another example (patsplat, 2003-09-13 13:08:08)
Okay, forget the config example. Let's say you have a cgi-script that you run in mod_ruby:
This script also exposes a library of Wiki functions, so when you require the file you get that api:
Why should it have to be named
for ruby to require it?
I think that it's reasonable:
I don't really see any added value with the rule that only .rb files can be required. I can see value to allowing ruby to require any text file (that doesn't end in .so)