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

Aly
  • 15,865
  • 47
  • 119
  • 191

4 Answers4

6

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'
Vasiliy Ermolovich
  • 24,459
  • 5
  • 79
  • 77
  • 1
    If he's running using ruby main.rb, it should no be the problem here – Adrien Rey-Jarthon Apr 03 '11 at 16:31
  • if he's running from the directory with main.rb file, otherwise it will be the problem here – Vasiliy Ermolovich Apr 03 '11 at 16:36
  • I am running from the directory with main.rb in it – Aly Apr 03 '11 at 17:42
  • 4
    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. – Jörg W Mittag Apr 04 '11 at 01:41
  • @Joerg: Is something like `$:.unshift File.join(File.dirname(__FILE__), "lib")` also a risk? – Andrew Grimm Apr 04 '11 at 07:14
  • @Andrew Grimm: No. Any attacker who would be able to inject files into that directory would probably be able to just edit the script anyway. The problem with adding `.` to the `$LOAD_PATH` is when *I* can get *you* to run your script from inside a directory that I own, I can inject pretty much arbitrary code into your script by e.g. putting a file named `sort.rb` in my directory, which will be found before yours. Especially if you use `Array#unshift` to put `.` at the *beginning* of `$LOAD_PATH`. – Jörg W Mittag Apr 05 '11 at 01:52
  • 1
    @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. – Jörg W Mittag Apr 05 '11 at 01:55
0

try require 'sort.rb' and check permissions

Adrien Rey-Jarthon
  • 1,015
  • 2
  • 9
  • 22
  • yes it should work, specifying the extension just help sometimes getting a better error description. especially for permissions or file-system problems – Adrien Rey-Jarthon Apr 03 '11 at 16:35
0

you would also:

require directory/sort.rb

Ray301
  • 296
  • 3
  • 15
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.

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338