views:

40

answers:

2

I've been trying to get a ruby file to require another ruby file and I feel like I'm going crazy. My setup is as follows

Two files in '/raid1/ruby-code/benchmark/'

CommandRunner
Benchmarker

Benchmarker is the main program at this point and wants to require CommandRunner, I've tried a bunch of different things and none of them work. The following is a list of all of the things I've put at the beginning of Benchmarker

require 'CommandRunner'
require './CommandRunner'
$LOAD_PATH.unshift File.expand_path(File.dirname($PROGRAM_NAME))
require 'CommandRunner'
$LOAD_PATH.unshift File.expand_path(File.dirname($PROGRAM_NAME))
require './CommandRunner'

I've also tried all of the above permutations using require_relative. I've tried it loading the file into irb inside of emacs, and I've tried it at the command line. At one point in irb it would load once with

require 'CommandRunner'
and then would load until I switched it back to './CommandRunner' and then it would load once again.

I've actually had the error say

`require_relative': no such file to load -- 
/raid1/ruby-code/benchmark/CommandRunner (LoadError)

which is the correct path to the file!

I've since switched it to load and that seems to be working, I'm seeing weird behavior but that just might be me. Does anyone have any idea what would be going on here?

+3  A: 

Is the name of the file CommandRunner and not CommandRunner.rb? The standard naming convention for ruby files is to use lowercase and underscores, so even though the class name would be CommandRunner, the file would be command_runner.rb, and then require 'command_runner' should work.

Ben Taitelbaum
The name of the file is CommandRunner, I might change it since I wasn't sure what standard practice was but right now that's the name of the file.
Andrew Myers
I can't find in the documentation that this specifically isn't supported. If you change CommandRunner to CommandRunner.rb, it'll work, but I think it might be a bug in eval.c that it's not finding the file CommandRunner (and worse, telling you the file doesn't exist).
Ben Taitelbaum
Ah, I think I found it. From http://ruby-doc.org/core/classes/Kernel.html#M005941 "... Otherwise, Ruby tries adding ``.rb'',``.so'', and so on to the name." So this specifies that if no extension is given, ruby will try adding valid extensions, so not having an extension is unspecified. In other words, no extension isn't considered valid if you want to require it.
Ben Taitelbaum
That was indeed the problem. I need to train myself to go to ruby-doc they way I go to the Hyperspec with Lisp. Thanks a lot!
Andrew Myers
A: 

In 99.999% of all cases, when a computer tells you it cannot find something, that is because that something isn't there.

So, the question is: does there exist a file named /raid1/ruby-code/benchmark/CommandRunner.rb?

Jörg W Mittag
...in the remaining 0.001% of cases, the computer is looking for a ninja...
zetetic