Ruby path on Windows for use in directory does not work

I have a small ruby ​​program in which require files are in the same directory. The program works fine on my mac, and when I run the test ruby ​​script without any requirements, it also works like this. The ruby ​​program does not seem to look in the current directory for the default file. for example a directory . . On windows where I need to update this so that Ruby looks in the current directory requiring?

+4
source share
3 answers

Well, now I understand that since version 1.9.2, for security reasons, they no longer allow this to work. The easiest way I found to solve this is weird was to put "./" before each requirement.

eg.

 require "./myfile.rb" 
+3
source

Most likely, your Mac works with Ruby 1.8, and Windows works with Ruby 1.9. Starting from 1.9, the default boot path no longer includes the current directory. A common practice is to add this to the top of your ruby ​​file before your requirements.

 $LOAD_PATH.unshift File.dirname(__FILE__) require 'my_file.rb' 

You can also use the abbreviated $: instead of $LOAD_PATH :

 $:.unshift File.dirname(__FILE__) 

Another alternative is to add a boot path on the command line:

 ruby -I. my_ruby_file.rb 
+7
source

"." was removed from $: was removed from Ruby 1.9.2 to be exact. Do

 puts RUBY_VERSION puts $:.inspect 

on Ruby 1.8 (which is installed on your Mac) and Ruby 1.9.2 (which is installed on your Windows machine) if you do not believe me.

Why Ruby 1.9.2 removes the "." from LOAD_PATH, and what is the alternative? discusses why. "has been deleted.

0
source

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


All Articles