RCR 149: require files that aren't .rb or .so
submitted by patsplat on 2003-07-24 20:32:04
This is a legacy RCR. If this is your RCR, please resubmit it using the new format and process.
It would be nice to be able to require files that don't end in '.rb'
Example: Say you want to share settings between ruby and perl.
ruby:
require 'my.settings'
perl:
do 'my.settings';
where 'my.settings' contains:
$tables = {
'person' => [ 'first', 'last' ],
'place' => [ 'city', 'state' ]
}
This is a neat trick, but it feels wrong for the settings to be '.rb'.
Cheers,
patsplat
Vote for this RCR
Add comments here
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)
Back to RCRchive.
RCR Submission page and RCRchive powered by Ruby, Apache, RuWiki (modified), and RubLog
loadovercomes this, but it doesn't do the "read this only once", whichrequiredoes implicitly.