Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Windows path for requires in dir not working

Tags:

windows

ruby

I've a small ruby program that require files in the same directory. Program works perfect on my mac and when I run a test ruby script without any require it also works so. It seems the ruby program doesn't look in the current directory for the file by default. e.g. the . dir. In windows where do I need to update this so ruby does look in the current dir for requires?

like image 419
Derek Organ Avatar asked Dec 28 '22 11:12

Derek Organ


2 Answers

Chances are that your Mac is running Ruby 1.8 and Windows is running Ruby 1.9. As of 1.9, the default load path no longer includes the current directory. A common practice is to add this to the top of your ruby file before your require statements

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

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

$:.unshift File.dirname(__FILE__)

Another alternative is adding the load path on the command line instead:

ruby -I. my_ruby_file.rb
like image 174
Dylan Markow Avatar answered Jan 04 '23 17:01

Dylan Markow


Ok, I understand now since 1.9.2 for "Security" reasons they don't allow require to work like that anymore. The neatest way I found to solve it strangely was to put './' in front of every require.

e.g.

require "./myfile.rb"
like image 28
Derek Organ Avatar answered Jan 04 '23 19:01

Derek Organ