21

How do I require a file from the current folder?

I have a file called sql_parser.rb that contains a class. I want to include this in another file also in the same folder, so I used:

require 'sql_parser'

That fails when I run from that folder:

LoadError: no such file to load -- sql_parser

I tried using IRB in the folder where this file exists and requiring it from there, but had the same issue.

Flip
  • 6,233
  • 7
  • 46
  • 75
Matt Roberts
  • 26,371
  • 31
  • 103
  • 180
  • 1
    See also http://stackoverflow.com/questions/2900370/why-does-ruby-1-9-2-remove-from-load-path-and-whats-the-alternative – knut Dec 14 '11 at 20:22

5 Answers5

33

In ruby 1.9.x, you can use the method require_relative. See http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-require_relative.

tomferon
  • 4,993
  • 1
  • 23
  • 44
8

Use

$:.unshift File.join(File.dirname(__FILE__))

to ensure that you're getting a path based on where the script is located, rather than where the program is being executed from. Otherwise, you're throwing away the security benefit gained by removing "." from the load path.

(Yes, the title of the question talks about requiring a file in the directory the script is being run from, but the body of the question mentions that the required file is in the same folder as the script in question)

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
  • Why is using "." less secure? Also, why not Dir.pwd instead? (it's more concise and accomplishes the same thing no?) – bkempner Dec 15 '11 at 00:34
  • @bkempner: Have you read the question linked to in the comments section of this question? – Andrew Grimm Dec 15 '11 at 00:43
  • @AndrewGrimm, +1 I can always rely on you to write well-informed and proper answers. Thanks for avoiding `require_relative`. To those that care, **this is the correct way to do load files** – maček Dec 06 '12 at 07:15
4

also...

require "./myRubyFile" # notice we exclude the .rb from myRubyFile.rb

jcpennypincher
  • 3,970
  • 5
  • 31
  • 44
3

On 1.9 the syntax changed to require_relative, as the other answers to this question said.

If you are writing in Ruby 1.8, I suggest writing your code in a forward looking manner, and using the require_relative gem, so you can use this keyword in your Ruby 1.8 and deal with one less transitional thing when you move to 1.9

RyanWilcox
  • 13,890
  • 1
  • 36
  • 60
2
$LOAD_PATH << Dir.pwd
require 'sql_parser'

As noted by @AndrewGrimm, due to security reasons, you should use this instead:

$LOAD_PATH << File.join(File.dirname(__FILE__))
require 'sql_parser'

Your directory has to be in the current load path. The load path is stored in a global array called $LOAD_PATH. If you append your current directory to it, then you can use require to load any files within the directory.

Using Dir.pwd instead of pwd.chomp as suggested by the Tin Man

Also I prefer this over require_relative unless you have a really small project otherwise things can get ugly.

bkempner
  • 652
  • 3
  • 8
  • Cheers. That works, but I thought ruby would always look in the pwd, am I wrong? – Matt Roberts Dec 14 '11 at 20:14
  • @Matt Roberts unfortunately not, but if you're working in a rails app all of that is handled for you. – bkempner Dec 14 '11 at 20:16
  • @MattRoberts: It depends on the ruby version. – knut Dec 14 '11 at 20:20
  • Ruby's `Dir.pwd` is probably more straightforward as it doesn't need to open a sub-shell or `chomp` the result. Also, with 1.9+, `require_relative './sql_parser'` or `require_relative 'sql_parser'` would be how I'd to go about it – the Tin Man Dec 14 '11 at 20:30
  • How is `$LOAD_PATH << Dir.pwd` any less unsecure than `$LOAD_PATH << "."`? At least with the latter you know that you're doing something unsecure. – Andrew Grimm Dec 14 '11 at 22:55
  • @knut true 1.8.7 require included current directory, i assumed he was using ruby 1.9.2. Trolls - why the down votes? If you down vote, at least explain your reasoning. I'd certainly like to know if there is a better approach. – bkempner Dec 15 '11 at 00:29
  • @bkempner: I downvoted. Would you rather I put "-1" in my comment? – Andrew Grimm Dec 15 '11 at 00:42
  • @AndrewGrimm Oh i see the issue now that i read the link. Thanks. – bkempner Dec 15 '11 at 03:42
  • I'm using ruby 1.9.2, so it looks like require_relative should do the trick for me - but this is interesting stuff. – Matt Roberts Dec 15 '11 at 08:17