Derek Organ
Derek Organ

Reputation: 8483

Ruby on Windows path for requires in dir not working

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?

Upvotes: 4

Views: 4162

Answers (3)

Derek Organ
Derek Organ

Reputation: 8483

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"

Upvotes: 3

Andrew Grimm
Andrew Grimm

Reputation: 81570

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

puts RUBY_VERSION
puts $:.inspect

on Ruby 1.8 (what's installed on your Mac) and Ruby 1.9.2 (what's installed on your windows machine) if you don't believe me.

Why does Ruby 1.9.2 remove "." from LOAD_PATH, and what's the alternative? discusses why "." was removed.

Upvotes: 0

Dylan Markow
Dylan Markow

Reputation: 124439

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

Upvotes: 7

Related Questions