Help with relative paths using require

T

typemismatch

Hi,

I'm having a lot of problems trying to "require" my own .rb files from
a lib folder in my project.

My structure is simple enough.

app\
app\lib
app\feature
app\feature2

I have all shared code in \lib and in any feature code I do a require
'..\lib\whatever.rb'

It runs fine locally but if I run on a server using Cron or a Daemon
the base path seems to be always set to something else so ..\lib
doesn't exist. My server and my dev. environments are very different
so it means hacking server deployments to find library files.

What is the correct method here? To wrap my lib code in a gem?

thanks!
-c
 
D

David A. Black

Hi --

Hi,

I'm having a lot of problems trying to "require" my own .rb files from
a lib folder in my project.

My structure is simple enough.

app\
app\lib
app\feature
app\feature2

I have all shared code in \lib and in any feature code I do a require
'..\lib\whatever.rb'

It runs fine locally but if I run on a server using Cron or a Daemon
the base path seems to be always set to something else so ..\lib
doesn't exist. My server and my dev. environments are very different
so it means hacking server deployments to find library files.

What is the correct method here? To wrap my lib code in a gem?

Does Tony Arcieri's require_rel (part of require_all) help?
http://github.com/tarcieri/require_all/tree/master


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2)
Training! Intro to Ruby, with Black & Kastner, September 14-17
(More info: http://rubyurl.com/vmzN)
 
D

David Masover

My structure is simple enough.

app\
app\lib
app\feature
app\feature2

I have all shared code in \lib and in any feature code I do a require
'..\lib\whatever.rb'

Quick question: Where are you actually running the app from? I'm guessing that
path isn't relative to the file the 'require' statement is in, but rather, from
the current working directory.
It runs fine locally but if I run on a server using Cron or a Daemon
the base path seems to be always set to something else so ..\lib
doesn't exist.

Among other things, I'm guessing you'll find the working directory is somewhere
else. But you could play with $: a bit.
What is the correct method here? To wrap my lib code in a gem?

That's a good idea anyway, but you could also do something like this:

require 'pathname'
$: << Pathname(__FILE__).parent.join('lib').to_s

Now, you could just do:

require 'whatever'

The .rb is implied, and the path includes 'lib' directly, now. You'd get
something similar if you packaged it as a gem.
 
T

typemismatch

Thanks David, the require_all was perfect! and easier than me
packaging gems :)

-c
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top