Refining the use of file joins or file expand_path

R

Robert Klemme

2010/2/2 Alpha Blue said:
Thanks for the clarification Robert and I like your distinction because
it makes more sense to me. =A0With your example in place, I am using
require and load statements correctly then. The only thing I've been
doing with regard to load statements is defining an absolute load path
rather than a relative load path. =A0Code execution in this example is th= e
loading of an xml source file (xrc) by wx_sugar.

Ah, OK. Then of course load is more appropriate.
This is strange because I don't know why I never thought about doing
this with Ruby. =A0I'm so used to keeping my projects separate and
therefore, I tend to keep the libs separate. =A0However, I could see
defining a libs dir for reusable library classes that I require in
future projects. =A0Then, all I would have to do is require and/or includ= e
the code I need. =A0Ocra does look over the rubyopt variables set at the
time of processing. =A0I'll have to check the $: to see that my custom li= b
directories are being set properly and included, but I can definitely
see this being a better way of implementing things later on. =A0I can
privatize and store all my libs on github for safekeeping and rdoc
everything for my own benefit.

Well, you can even have a mix of both: you can set RUBYLIB to your
"shared libs" folder and then in every main executable add an
appropriate folder of your local project. And we're back to the
original question IIRC. :) If you have to do that on multiple
machines though then it might be reasonable to use gems for
distribution and use the gems mechanism as storage for the shared
libs. But the principle would be the same.
It does - my thanks!

You're welcome!

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
A

Alpha Blue

Robert said:
Well, you can even have a mix of both: you can set RUBYLIB to your
"shared libs" folder and then in every main executable add an
appropriate folder of your local project. And we're back to the
original question IIRC. :) If you have to do that on multiple
machines though then it might be reasonable to use gems for
distribution and use the gems mechanism as storage for the shared
libs. But the principle would be the same.

Hi Robert, I just wanted to thank you once again for your advice. I
created a custom libs folder yesterday and moved a lot of source code
with regard to public instance methods for the main gui objects I work
with. It took some time to set it all up, but it's working great. I
even created a rake task that pulls in the shared lib documentation for
classes I'm using in the project so that it's also built with my app.

The only special event I did with my main lib folder was set an absolute
lib path:

$lib_path = __FILE__

.. so that when I do have to use load methods from the RUBYLIB location,
the file path can still be maintained for the working project directory.

def set_icon_file(path, *file)
icon_file = File.join(File.dirname(path), file)
set_icon Wx::Icon.new(icon_file, Wx::BITMAP_TYPE_ICO)
end

and I can then use set_icon_file($lib_path, "icons", "test.ico")

.. so everything is working fine now with regard to using my custom libs
with my project libs. I'm only placing methods and classes that will
absolutely be marked as reusable code.

My thanks again.
 
A

Albert Schlef

Alpha said:
The question I have (because I have written used both) is which would
"you" use and why keeping the following in mind...

Dir[File.join(File.dirname(__FILE__),"..","ui","*.rb")].each {|rb_files|
load rb_files}

BTW, there's an alternative to explicit use of File.join() which makes
the code more aesthetic:

To define String#/, like this:

class String
def /(o)
File.join(self, o.to_s)
end
end

and then you can do:

Dir[File.dirname(__FILE__) / ".." / "ui" / "*.rb"]

or even:

Dir[File.dirname(__FILE__) / ".." / :some / :path / "*.rb"]

I once investigated whether File.join() is needed and got the impression
that one can just use '/' and things would work. Also, see this:

http://rubyforge.org/tracker/index.php?func=detail&aid=13026&group_id=426&atid=1698

Yet I see some Ruby gurus using File.join().
Dir[File.join(File.dirname(__FILE__),"..","ui","*.rb")].each {|rb_files|
load rb_files}

There's a problem in this approach: if you have a class the inherits
from a base class, you need to load the base class first. Dir[] gives
you random order(?), which is bad.
 
A

Albert Schlef

Robert said:
Turning back to your original example: it looks like what you really
want is to do "require 'ui'"

I don't think it's safe to do "require 'ui'". Some other library may a
have a file by the same name and Ruby may load it instead of ours.

So one should do "require 'mylibrary/ui'" instead.

(Or one could feed require() an absolute path, but I don't see people
doing this.)

Alpha said:
Also, if you accidentally nest a require, say for example:

--root\
-- app.rb
----\lib
---- main.rb
---- modules.rb

.. note that modules.rb and main.rb are within the same directory
(main).

If your app.rb has require 'lib/main'
.. and
If your main.rb has require 'modules'

Same problem. If some other library has 'modules.rb', Ruby may load if
instead of ours.

The directory structure should be:
--root\
-- app.rb
----\lib
-------myapp.rb (instead of 'main.rb')
-------\myapp
------- modules.rb

app.rb should push Dir.basename(__FILE__) + '/lib' onto $: (if it's not
already there). Then, it should require 'myapp.rb'. myapp.rb should
'require "myapp/modules.rb"'. Then there's no risk that Ruby will load
other library's files because, by having a 'myapp' subdir were
effectively creating a namespace.
 
A

Alpha Blue

Albert said:
I don't think it's safe to do "require 'ui'". Some other library may a
have a file by the same name and Ruby may load it instead of ours.

So one should do "require 'mylibrary/ui'" instead.

I use private namespaces for my own libraries. The provided examples
were simply put, examples. It's good to showcase what is proper though,
and I'm sure someone else that may read this down the road will find it
useful.

With GUI programs, especially those that may be unpacked in a temp
directory, I only use absolute paths pertaining to the unpacked files.
For all other files I use Dir.getwd which provides the current working
directory.

Thanks.
 
R

Robert Klemme

I don't think it's safe to do "require 'ui'". Some other library may a
have a file by the same name and Ruby may load it instead of ours.

So one should do "require 'mylibrary/ui'" instead.

That was based on the example given and under the assumption that the
library's name is "ui". In that case you would use reqire 'ui'.

Cheers

robert
 

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,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top