Ruby error

johncosentino
johncosentino used Ask the Experts™
on
I'm getting the following error message when trying to run a ruby script.  I listed the beginning of the script for reference.  Please help.


<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- exceptions (LoadError)
        from <internal:lib/rubygems/custom_require>:29:in `require'
        from ./ssh_helpers.rb:6:in `<main>'

-------------------------SCRIPT IS BELOW-------------------------------------

#!/usr/bin/env ruby
require 'rubygems'
require 'open4'
require 'socket'

require 'exceptions'


Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Andrew DoadesIT Manager

Commented:
Its this line that's causing the issue:

require 'exceptions'

What are you trying to require? is this a gem or a separate ruby file?

Andrew
Andrew DoadesIT Manager

Commented:
To Clarify what the error message is, its trying to explain, it doesn't know what 'exceptions' is - it isn't an installed gem, nor is there an 'exceptions.rb' file in the same directory as this script

Author

Commented:
This is a separate ruby file, and I confirmed that the exceptions.rb file is in the same directory as the script in question.
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Author

Commented:
So I've found a fix, though I'm still not sure what causes the problem.

if you change the line in the script from require 'exceptions' to require_relative 'exceptions' the problem is resolved.

Apparently the "_relative" tells the script to look in the current directory?  
Andrew DoadesIT Manager

Commented:
The only thing I can think off, is that ruby is trying to use system call with it being 'exceptions'?
One other thing you could try would be to rename the file to 'my_exceptions.rb' and then
require 'my_exceptions'

By using _relative you're forcing ruby to check the current directory first.

Author

Commented:
This might be a stupid question, but why wouldn't it check the current directory first with just "require"....what does "require" check first?
Andrew DoadesIT Manager

Commented:
Are you running this script from within the directory its located?

Have you done something like this:
cd /ruby_files/
ruby test.rb

So when you run the ruby file, you're running it from the same directory?

or are you running it like this:
ruby ruby_files/test.rb
Commented:
ruby sets up a number of directories that it looks in for locating files that are required.  You can see what that is on your system by typing:
   ruby -e 'puts $:'
(The $: is a global variable that contains the load paths for load / require expressions)

It's kind of a linux convention to not  automatically look in the current directory; this avoids the scenario where someone puts a malicious file in your current directory to has the same name as another command and therefore gets executed when you run that command. That's why in Linux (or Mac Terminal), you have to type:
   ./command_name
to run something from the current directory.

And that's why there's require_relative... to make it easier to access the current directory for loaded or required files.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial