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?

- 25,510
- 39
- 140
- 297

- 8,323
- 17
- 56
- 75
-
The original [select isn't broken](http://pragprog.com/the-pragmatic-programmer/extracts/tips) anecdote was about someone assuming that something was wrong with Windows. (The link is only to an explanation of "select isn't broken" - the anecdote is in the book itself) – Andrew Grimm Nov 18 '11 at 00:23
-
I own that book and have read it. Its really doesn't apply here. Ruby 'app' is surely portable between OS. I'd like to know is there a config setting somewhere for Ruby on windows that will automatically look in the local dir for required files. I'm not claiming there is a fault with the OS just a question on how to config – Derek Organ Nov 18 '11 at 01:49
-
1@DerekOrgan: Just because there are runtimes for both platforms doesn't mean that the "app is surely portable between OS". When you take advantage of system-specific behavior, such as the library search path, you lose portability. And changing the ruby file to not rely on non-portable behavior is exactly the right solution. – Ben Voigt Nov 18 '11 at 02:27
-
Its a relative path specifically for the folder my ruby app is in not system specific. I accept that it was changed for a good reason but in general relative paths should be portable. – Derek Organ Nov 18 '11 at 02:39
3 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

- 123,080
- 26
- 284
- 201
-
the idea of adding anything to the ruby file to make it work seems very flawed. – Derek Organ Nov 18 '11 at 01:45
-
1Another option is using `require_relative` instead. But they removed the current directory in Ruby 1.9 from the default load path for security reasons. – Dylan Markow Nov 18 '11 at 02:17
-
-
@Derek Organ: Why flawed? You are using different Ruby versions, so you should expect having to adjust the code. Btw. the proposed change is compatible with 1.8. – undur_gongor Nov 18 '11 at 08:06
-
I accept that now, at the time I ask the question didn't realize it was a different version on both machines. – Derek Organ Nov 18 '11 at 12:53
-
@DylanMarkow `require_relative` is the best answer I've seen --- add it to your answer! :) – Niels Abildgaard Aug 20 '14 at 18:01
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"

- 8,323
- 17
- 56
- 75
"."
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.

- 1
- 1

- 78,473
- 57
- 200
- 338