Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ruby

rubygems

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?

like image 892
GreenTriangle Avatar asked Aug 11 '26 21:08

GreenTriangle


1 Answers

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.

like image 176
ptierno Avatar answered Aug 13 '26 21:08

ptierno



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!