Comment on this RCR (edit wiki page) | RCRchive home

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

Strongly opposed [3]
Opposed [0]
Neutral [1]
In favor [1]
Strongly advocate [0]

Change the status of this RCR to:

accepted

rejected

withdrawn


Add comments here

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", which require 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:


    
  load '~/.config'

    

then use:


    
  require 'load_config'

    

Some advantages of this solution over simply using load 'config':

Sharing configuration (lennon, 2003-07-29 07:27:50)

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).

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:

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:

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