4

I have the following two files: main.rb and sort.rb located in the same folder. In main.rb I have the following code:

require 'sort'

Sort.insertion_sort([1,2,3,4]).each {|x| print "#{x}, "}

When I try and run this via ruby main.rb I get the following error:

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

Any ideas why? Thanks

3
  • weird it works on mine. what does sort.rb contain? Commented Apr 3, 2011 at 16:30
  • i scrapped sort and tried to require rexml/syncenumerator and got the same error Commented Apr 3, 2011 at 17:41
  • Related question: stackoverflow.com/questions/2900370/… Commented Apr 4, 2011 at 7:24

4 Answers 4

5

The better way to use

require_relative "sort"

intead of

require "sort"

Thanks, @Jörg W Mittag.

Or you can add a path where ruby should search your files (can be a security risk):

$:.unshift File.join(File.dirname(__FILE__), ".") # current directory
require 'sort'
Sign up to request clarification or add additional context in comments.

7 Comments

If he's running using ruby main.rb, it should no be the problem here
if he's running from the directory with main.rb file, otherwise it will be the problem here
I am running from the directory with main.rb in it
Please do not ever do this. There is a reason why . was removed from the $LOAD_PATH. If you want to require a file relative to the location of the currently executing file, use require_relative, that's what it's there for.
@Andrew Grimm: Note, however, that this sort of relative $LOAD_PATH manipulation is still a smell, since your package manager should do that for you. E.g. RubyGems automatically adds your Gem's lib directory, or any other directory/ies you specify to the $LOAD_PATH anyway, as does Bundler, and if you use some other package management system, then it's the packager's and/or the system administrator's job to do that. You shouldn't need to do that yourself.
|
0

try require 'sort.rb' and check permissions

1 Comment

yes it should work, specifying the extension just help sometimes getting a better error description. especially for permissions or file-system problems
0

you would also:

require directory/sort.rb

1 Comment

main is in the same directory as sort.rb
0

In Ruby 1.9.2, $: doesn't include the current directory ('.'). Either do relative_require instead, or do $: << '.'.

Joerg Mittag says that $: << '.' shouldn't be done because it's a security risk.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.