Find::Bin, use lib, in ruby

Hi,

problem:

I have my own set of libraries located relative to my script in …/lib
i.e. my configuration is something like

rubyscripts/bin/script.rb
rubyscripts/lib/libforscript.rb

How can I tell script.rb where to look for libforscript.rb without
having to use absolute paths?

db

···


Mar 19 Swallows return to Capistrano
Mar 19 St. Joseph’s Day, obeserved in Colombia, Costa Rica, Holy See,
Liechtenstein, San Marino, Spain, Venezuela
Mar 19 Tree Planting Day in Lestho

$:.unshift “…/lib”
require “libforscript.rb”

···

On Thu, Mar 20, 2003 at 03:03:09AM +0900, Daniel Bretoi wrote:

rubyscripts/bin/script.rb
rubyscripts/lib/libforscript.rb

How can I tell script.rb where to look for libforscript.rb without
having to use absolute paths?

In bin/script.rb:
$: << “…/lib”

Now you can:
require ‘libforscript’

-austin
– Austin Ziegler, austin@halostatue.ca on 2003.03.19 at 13:33:42

···

On Thu, 20 Mar 2003 03:03:09 +0900, Daniel Bretoi wrote:

problem:

I have my own set of libraries located relative to my script in
…/lib i.e. my configuration is something like

rubyscripts/bin/script.rb
rubyscripts/lib/libforscript.rb

How can I tell script.rb where to look for libforscript.rb without
having to use absolute paths?

in script.rb do (on *nix systems)

prefix = File.dirname (FILE) << ‘/…/lib/’
require (prefix + ‘libforscript’)

-a

···

On Thu, 20 Mar 2003, Daniel Bretoi wrote:

Hi,

problem:

I have my own set of libraries located relative to my script in …/lib
i.e. my configuration is something like

rubyscripts/bin/script.rb
rubyscripts/lib/libforscript.rb

How can I tell script.rb where to look for libforscript.rb without
having to use absolute paths?

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

Daniel Bretoi lists@debonair.net writes:

Hi,

problem:

I have my own set of libraries located relative to my script in …/lib
i.e. my configuration is something like

rubyscripts/bin/script.rb
rubyscripts/lib/libforscript.rb

How can I tell script.rb where to look for libforscript.rb without
having to use absolute paths?

Adam Spiers, the co-developer of RubyPhoto, wrote a library to do this
that is included as part of the RubyPhoto distribution. Grab
abspath.rb and findbin.rb from:

http://www.plig.net/~ture/rubyphoto/rubyphoto-latest/

and stick them in your site library directory. You should then be
able to do things like:

require ‘findbin’
$:.push FindBin::RealBin + “/…/lib”

in your script, similar to how the Perl module works. It even handles
symlinks the same as the Perl one, IIRC.

At some point he or I might even write documentation for it and parcel
it up separately, though it’s not a terribly big module, so you should
be able to work out what’s going on.

···


Mark Hulme-Jones < ture@plig.net >

what if you were in /usr/bin/ while running script.rb? the ‘…/lib’ needs to
be relative to the location of script.rb, not the current working directory.

-a

···

On Thu, 20 Mar 2003, Brian Candler wrote:

On Thu, Mar 20, 2003 at 03:03:09AM +0900, Daniel Bretoi wrote:

rubyscripts/bin/script.rb
rubyscripts/lib/libforscript.rb

How can I tell script.rb where to look for libforscript.rb without
having to use absolute paths?

$:.unshift “…/lib”
require “libforscript.rb”

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

I’ve run into this problem before, i.e. your script is stored in a different
location to the current dir, and requires a file from the same directory as
itself. I simply put this at the top of my files:

$LOAD_PATH << File.dirname(FILE.gsub(/\/, ‘/’))

The gsub is needed on Windows systems because dirname only works with
forward slashes (is there any plan to fix this it’s a pain).

(I also prefer using $LOAD_PATH because I hate all that $: nonsence… its
completely unreadable)

Alan

“ahoward” ahoward@fsl.noaa.gov wrote in message
news:Pine.LNX.4.53.0303201609120.1795@eli.fsl.noaa.gov

rubyscripts/bin/script.rb
rubyscripts/lib/libforscript.rb

How can I tell script.rb where to look for libforscript.rb without
having to use absolute paths?

$:.unshift “…/lib”
require “libforscript.rb”

what if you were in /usr/bin/ while running script.rb? the ‘…/lib’ needs
to
be relative to the location of script.rb, not the current working
directory.

···

On Thu, 20 Mar 2003, Brian Candler wrote:

On Thu, Mar 20, 2003 at 03:03:09AM +0900, Daniel Bretoi wrote:

-a

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

Yes, good point. What I often actually use is:

require ‘/etc/my.conf.rb’

and in there put

$:.unshift ‘/path/to/lib’

whilst developing a particular library.

I use $: only because I came across it in the Pickaxe before $LOAD_PATH, and
because I (wrongly) thought I’d have to “require ‘English’” to get it.

Cheers,

Brian.

···

On Fri, Mar 21, 2003 at 01:37:40AM +0900, ahoward wrote:

$:.unshift “…/lib”
require “libforscript.rb”

what if you were in /usr/bin/ while running script.rb? the ‘…/lib’ needs to
be relative to the location of script.rb, not the current working directory.