Aly
Aly

Reputation: 16285

Ruby - LoadError on require

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

Upvotes: 4

Views: 15761

Answers (4)

Vasiliy Ermolovich
Vasiliy Ermolovich

Reputation: 24627

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'

Upvotes: 5

Andrew Grimm
Andrew Grimm

Reputation: 81631

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.

Upvotes: 0

Ray301
Ray301

Reputation: 296

you would also:

require directory/sort.rb

Upvotes: 0

Adrien Rey-Jarthon
Adrien Rey-Jarthon

Reputation: 1174

try require 'sort.rb' and check permissions

Upvotes: 0

Related Questions