11

I have a Ruby code with different classes in a few files. In one file, I start the execution. This file requires my other files.

  • Is this a good way to start a ruby code?
  • When I run the code from a symbolic link, for example DIR2/MyRubyCode is a link to the main file DIR1/MyRubyCode.rb, then my requires will fail. I solved the problem by adding the path DIR1 to $LOAD_PATH before the require, but I think there would be much better ways to do it. Do you have any suggestions about that?
sawa
  • 165,429
  • 45
  • 277
  • 381
Cedric H.
  • 7,980
  • 10
  • 55
  • 82

3 Answers3

18

If you're using Ruby 1.9 or greater, user require_relative for your dependencies.

require_relative 'foo_class'
require_relative 'bar_module'
Alex V
  • 18,176
  • 5
  • 36
  • 35
16

If you want to check if a Ruby file is being 'require'ed or executed with 'ruby MyRubyCode.rb', check the __FILE__ constant.

# If the first argument to `ruby` is this file.
if $0 == __FILE__
  # Execute some stuff.
end

As far as the require/$LOAD_PATH issue, you could always use the relative path in the require statement. For example:

# MyRubyCode.rb
require "#{File.dirname(__FILE__)}/foo_class"
require "#{File.dirname(__FILE__)}/bar_module"

Which would include the foo_class.rb and bar_module.rb files in the same directory as MyRubyCode.rb.

Dale Campbell
  • 496
  • 5
  • 10
  • 1
    Thanks but if I require with File.dirname(__FILE__), when I launch the code using an alias it also tries to include from '.' and I still have the 'no such file to load' error. – Cedric H. Aug 29 '10 at 17:21
  • 1
    That is correct. You would have to get the target of the symlink (which would go in place of `File.dirname()`). Check out this post for a couple different ways of doing that: http://stackoverflow.com/questions/1237939/ruby-how-do-i-get-the-target-of-a-symlink – Dale Campbell Aug 29 '10 at 17:36
  • 2
    Thanks. It works well with `$LOAD_PATH << File.dirname(Pathname.new(File.expand_path(__FILE__)).realpath.to_s).to_s` . – Cedric H. Aug 30 '10 at 07:18
  • 1
    For me it works only without string interpolation: `require File.dirname(__FILE__) + "/foo_class"` – mmj Jun 19 '12 at 01:35
  • 3
    use `require_relative` instead –  May 04 '14 at 15:53
7

I know this is an old question, but there is an updated answer to it, and I wanted to post it:

Starting in a more recent version of Ruby (I'm not sure when), you can require files in the same directory by using the following:

require './foo_class'
require './bar_module'

and it'll load files called foo_class.rb and bar_module.rb in the same directory.

For checking if your file is being required or ran normally, check the other answer.

Piccolo
  • 1,612
  • 4
  • 22
  • 38
  • 2
    This answer does not address the use case in the original question, because it will still fail if the ruby file is being invoked from a symlink or is on the user's path. – Nathan Dec 02 '13 at 19:03
  • This may not directly address the original question but it *does* come up as the first hit on Google when you're trying to figure out the right way to require files relative to a symlink. require_relative is smart enough to know if it's a symlink and will go back relative to the original file whereas require "./" will require relative to the symlink itself, which is just what I needed. – TedMilker Mar 03 '22 at 13:59