Ruby require "no such file to load" error yet clearly in path
Asked Answered
L

1

11

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?

Lumbard answered 28/10, 2010 at 0:28 Comment(0)
U
6

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.

Updraft answered 28/10, 2010 at 0:34 Comment(4)
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.Lumbard
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).Updraft
Ah, I think I found it. From 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.Updraft
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!Lumbard

© 2022 - 2024 — McMap. All rights reserved.