Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Refining the use of file joins or file expand_path

Reply
Thread Tools

Refining the use of file joins or file expand_path

 
 
Alpha Blue
Guest
Posts: n/a
 
      01-30-2010
Both bits of code process the same result which is finding .rb file
extensions within a specified relative path.

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

Ruby 1.9.x
Cross - OS usability (windows/linux/mac/etc.)

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

Dir[File.expand_path('../../ui/*.rb', __FILE__)].each {|rb_files| load
rb_files}

Thanks.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      01-30-2010
El S=C3=A1bado, 30 de Enero de 2010, Alpha Blue escribi=C3=B3:
> Both bits of code process the same result which is finding .rb file
> extensions within a specified relative path.
>=20
> The question I have (because I have written used both) is which would
> "you" use and why keeping the following in mind...
>=20
> Ruby 1.9.x
> Cross - OS usability (windows/linux/mac/etc.)
>=20
> Dir[File.join(File.dirname(__FILE__),"..","ui","*.rb")].each {|rb_files|
> load rb_files}
>=20
> Dir[File.expand_path('../../ui/*.rb', __FILE__)].each {|rb_files| load
> rb_files}


I prefer the first one.


=2D-=20
I=C3=B1aki Baz Castillo <(E-Mail Removed)>

 
Reply With Quote
 
 
 
 
Alpha Blue
Guest
Posts: n/a
 
      01-30-2010
Iñaki Baz Castillo wrote:
> I prefer the first one.


I do too, mainly because I don't like to use slashes.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Ryan Davis
Guest
Posts: n/a
 
      01-31-2010

On Jan 30, 2010, at 12:33 , Alpha Blue wrote:

> Both bits of code process the same result which is finding .rb file
> extensions within a specified relative path.
>=20
> The question I have (because I have written used both) is which would
> "you" use and why keeping the following in mind..


Neither. I write sane code with sane requires and sane load paths. I =
also NEVER glob my requires.

> Ruby 1.9.x
> Cross - OS usability (windows/linux/mac/etc.)
>=20
> Dir[File.join(File.dirname(__FILE__),"..","ui","*.rb")].each =

{|rb_files|
> load rb_files}
>=20
> Dir[File.expand_path('../../ui/*.rb', __FILE__)].each {|rb_files| load
> rb_files}


My version:

> require "x/ui/blah1"
> require "x/ui/blah2"


assuming:

> lib/x/ui/blah[12].rb


It is 100% easier to read, maintain, and understand. There's no magic, =
and nothing to debug because you don't pull in stuff you didn't expect.


 
Reply With Quote
 
Alpha Blue
Guest
Posts: n/a
 
      01-31-2010
Ryan Davis wrote:
> My version:
>
>> require "x/ui/blah1"
>> require "x/ui/blah2"

>
> assuming:
>
>> lib/x/ui/blah[12].rb

>
> It is 100% easier to read, maintain, and understand. There's no magic,
> and nothing to debug because you don't pull in stuff you didn't expect.


That doesn't work for all operating systems.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
pharrington
Guest
Posts: n/a
 
      01-31-2010
On Jan 30, 9:24*pm, Alpha Blue <(E-Mail Removed)> wrote:
> Ryan Davis wrote:
> > My version:

>
> >> require "x/ui/blah1"
> >> require "x/ui/blah2"

>
> > assuming:

>
> >> lib/x/ui/blah[12].rb

>
> > It is 100% easier to read, maintain, and understand. There's no magic,
> > and nothing to debug because you don't pull in stuff you didn't expect.

>
> That doesn't work for all operating systems.
>
> --
> Posted viahttp://www.ruby-forum.com/.


It'll at least work on Windows, Linux, and OSX. For what OSs will
forward slashes in the path not work?
 
Reply With Quote
 
pharrington
Guest
Posts: n/a
 
      01-31-2010
On Jan 30, 10:45*pm, pharrington <(E-Mail Removed)> wrote:
> On Jan 30, 9:24*pm, Alpha Blue <(E-Mail Removed)> wrote:
>
>
>
>
>
> > Ryan Davis wrote:
> > > My version:

>
> > >> require "x/ui/blah1"
> > >> require "x/ui/blah2"

>
> > > assuming:

>
> > >> lib/x/ui/blah[12].rb

>
> > > It is 100% easier to read, maintain, and understand. There's no magic,
> > > and nothing to debug because you don't pull in stuff you didn't expect.

>
> > That doesn't work for all operating systems.

>
> > --
> > Posted viahttp://www.ruby-forum.com/.

>
> It'll at least work on Windows, Linux, and OSX. For what OSs will
> forward slashes in the path not work?


Not that that's even really the point, though :\
 
Reply With Quote
 
Alpha Blue
Guest
Posts: n/a
 
      01-31-2010
pharrington wrote:
>
> It'll at least work on Windows, Linux, and OSX. For what OSs will
> forward slashes in the path not work?


Tops-20
OpenVMS
Classic MAC OS
RISC OS
Symbian OS
Stratus VOS
etc.

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'

...


(in E:/Gui-Development/GuiTemplate)
ruby app.rb
E:/Gui-Development/GuiTemplate/lib/main.rb:2:in `require': no such file
to load
-- modules (LoadError)
from E:/Gui-Development/GuiTemplate/lib/main.rb:2:in `<top
(required)>'
from app.rb:8:in `require'
from app.rb:8:in `<main>'
rake aborted!
Command failed with status (1): [ruby app.rb...]

(See full trace by running task with --trace)

.. now take the same require 'modules' out and place:

require File.join(File.dirname(__FILE__),"modules")

.. and no errors.

main.rb and modules.rb are relative to each other. But, if you start an
application from a different directory, the require has to have a
defined path.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Ryan Davis
Guest
Posts: n/a
 
      01-31-2010

On Jan 30, 2010, at 18:24 , Alpha Blue wrote:

> Ryan Davis wrote:
>> My version:
>>=20
>>> require "x/ui/blah1"
>>> require "x/ui/blah2"

>>=20
>> assuming:
>>=20
>>> lib/x/ui/blah[12].rb

>>=20
>> It is 100% easier to read, maintain, and understand. There's no =

magic,=20
>> and nothing to debug because you don't pull in stuff you didn't =

expect.
>=20
> That doesn't work for all operating systems.


As pointed out by others... yes... yes it does. For ruby's definition of =
"all" that is.


 
Reply With Quote
 
Ryan Davis
Guest
Posts: n/a
 
      01-31-2010

On Jan 30, 2010, at 20:06 , Alpha Blue wrote:

> pharrington wrote:
>>=20
>> It'll at least work on Windows, Linux, and OSX. For what OSs will
>> forward slashes in the path not work?

>=20
> Tops-20
> OpenVMS
> Classic MAC OS
> RISC OS
> Symbian OS
> Stratus VOS
> etc.


uh... wuh?

> Also, if you accidentally nest a require, say for example:
>=20
> --root\
> -- app.rb
> ----\lib
> ---- main.rb
> ---- modules.rb
>=20
> .. note that modules.rb and main.rb are within the same directory=20
> (main).
>=20
> If your app.rb has require 'lib/main'
> .. and
> If your main.rb has require 'modules'


OK. Stop. Let's just stop right there. you obviously overlooked the part =
where I said SANE.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Some help in refining this regex for CSV files Oltmans Python 2 12-06-2012 02:27 PM
Kernel.exec won't find a file that File.expand_path can Fritz Anderson Ruby 3 12-09-2008 05:30 PM
Refining the popup? George Hester Javascript 4 10-15-2004 09:30 PM
File.expand_path(__FILE__) John Platte Ruby 29 05-03-2004 08:20 AM
File.expand_path and ~ on windows Alan Davies Ruby 2 09-22-2003 04:20 PM



Advertisments