0

I was reading a gemspec file and the directory looks like

gem/
  lib/
  a.rb
  gem.gemspec

In the gemspec there is a line

$:.push File.expand_path('../lib', __FILE__)

I suppose it means adding the lib folder to Ruby's load path so in there you can require stuff.

But why ../lib not simply lib?

Using the same example as above, if you want to load the code in a.rb, don't you just do require 'a' instead of require '../a'?

Notice both lib and a.rb have the same relative position from gemspec.

1 Answers1

0

When using require, you simple have to either add the required file like:

require 'yourfile'

For the above example, you would simply have to have the required file in the folder you are working out of. If it is a gem, it doesn't matter, you can simply add the require statement. What you are seeing is how the gem knows to be included with the require without having to specify the folder. When you use a gem you generally install it, then require it, without specifying where you are getting it from.

The other option is to put the required file in another folder, then specify the path, to that file/folder:

require './yourfolder/yourfile'
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
  • I was more asking about how the internals of a gem `require` each other rather than using the gem itself. The comment under my question pointed me to the right place though. Thanks anyway for your answer. –  Feb 14 '13 at 20:39