GreenTriangle
GreenTriangle

Reputation: 2460

How does an executable within a gem reference the greater gem?

Say that my gem is VideoPlayer. The folders tructure is:

VideoPlayer/
    /bin
        vidplay.rb
    /lib
        VideoPlayer.rb
        Subtitler.rb
        Screenshotter.rb

I want people to invoke vidplay from the command line, and for vidplay to reference code in the VideoPlayer, Subtitler and Screenshotter files.

If I just write, within vidplay.rb, require '../lib/VideoPlayer.rb', it will throw an error, saying that it cannot require such file. I thought "Maybe it automatically requires everything in lib/", but it apparently doesn't; if I don't require anything, it'll say that VideoPlayer is an uninitialised constant.

So how does this work?

Upvotes: 3

Views: 30

Answers (1)

ptierno
ptierno

Reputation: 10074

I usually add the lib dir to the library load path ($:). You can add this to the top of your bin file.

lib = File.expand_path('../../lib', __FILE__)
$:.unshift(lib) unless $:.include?(lib)

Then you can do a normal require:

require 'videoplayer'

Hope this helps.

Upvotes: 1

Related Questions