Ruby - LoadError on Demand

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 to run this through 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

+4
source share
4 answers

Best way to use

 require_relative "sort" 

int

 require "sort" 

Thanks, @ Jörg W Mittag.

Or you can add a path where ruby ​​should look for your files (this could be a security risk):

 $:.unshift File.join(File.dirname(__FILE__), ".") # current directory require 'sort' 
+6
source

try require 'sort.rb' and check permissions

0
source

you also:

require directory/sort.rb

0
source

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

Jörg Mittag says $: << '.' should not be done because it is a security risk.

0
source

Source: https://habr.com/ru/post/1346456/


All Articles