ruby picture

RCR 149: require files that aren't .rb or .so

Submitted by patsplat (Fri Jul 25 00:07:04 UTC 2003)

Abstract

This is a legacy RCR from Ruby Garden, submitted by patsplat. Matz has declared these RCRs obsolete, and asked that their authors resubmit them in the new format.

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

Problem

(RCR imported from old format)

Proposal

Analysis

(RCR imported from old format)

Implementation

(RCR imported from old format)
ruby picture
Comments Current voting

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':

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

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:

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


Strongly opposed 0
Opposed 0
Neutral 0
In favor 0
Strongly advocate 0
ruby picture
If you have registered at RCRchive, you may now sign in below. If you have not registered, you may sign up for a username and password. Registering enables you to submit new RCRs, and vote and leave comments on existing RCRs.
Your username:
Your password:

ruby picture

Powered by .