[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

"require" with many files

Iñaki Baz Castillo

4/29/2008 8:58:00 AM

SGksIGlzIHRoZXJlIGFueSBwcmVmZXJyZWQgd2F5IHRvIGxvYWQvcmVxdWlyZSBhbGwgdGhlIGZp
ZWxzIGluIGEgZGlyZWN0b3J5PwpGb3IgZXhhbXBsZSwgbXkgbWFpbiBmaWxlIGRvZXM6CgogIHJl
cXVpcmUgImhlYWRlci5yYiIKCmFuZCBmaWxlICJoZWFkZXIucmIiIG11c3QgbG9hZCBhbGwgdGhl
IHJlbGF0ZWQgZmlsZXMgY29udGFpbmVkIGluCiJoZWFkZXJzLyIgZGlyZWN0b3J5LiBJIGRvIHRo
ZSBmb2xsb3dpbmcgaW4gImhlYWRlci5yYiI6CgogIGhlYWRlcnNfcGF0aCA9IEZpbGUuZGlybmFt
ZShfX0ZJTEVfXykgKyAnL2hlYWRlcnMnCiAgRGlyLmNoZGlyKGhlYWRlcnNfcGF0aCkKICBEaXJb
IioucmIiXS5lYWNoIGRvIHxmaWxlfAogICAgICByZXF1aXJlICIje2hlYWRlcnNfcGF0aH0vI3tm
aWxlfSIKICBlbmQKCklzIGl0IGVub3VnaHQgb2sgb3IgY2FuIGl0IHJlY2VpdmUgc29tZSBSdWJ5
J3MgbWFnaWM/IFhECgpUaGFua3MgZm9yIGFueSBzdWdnZXN0aW9uLgoKLS0gCknDsWFraSBCYXog
Q2FzdGlsbG8KPGliY0BhbGlheC5uZXQ+Cg==

6 Answers

Roger Pack

4/29/2008 4:59:00 PM

0

SSd2ZSBuZXZlciBzZWVuIGFueXRoaW5nIGVsc2UKCk9uIFR1ZSwgQXByIDI5LCAyMDA4IGF0IDI6
NTggQU0sIEnDsWFraSBCYXogQ2FzdGlsbG8gPGliY0BhbGlheC5uZXQ+IHdyb3RlOgo+IEhpLCBp
cyB0aGVyZSBhbnkgcHJlZmVycmVkIHdheSB0byBsb2FkL3JlcXVpcmUgYWxsIHRoZSBmaWVscyBp
biBhIGRpcmVjdG9yeT8KPiAgRm9yIGV4YW1wbGUsIG15IG1haW4gZmlsZSBkb2VzOgo+Cj4gICBy
ZXF1aXJlICJoZWFkZXIucmIiCj4KPiAgYW5kIGZpbGUgImhlYWRlci5yYiIgbXVzdCBsb2FkIGFs
bCB0aGUgcmVsYXRlZCBmaWxlcyBjb250YWluZWQgaW4KPiAgImhlYWRlcnMvIiBkaXJlY3Rvcnku
IEkgZG8gdGhlIGZvbGxvd2luZyBpbiAiaGVhZGVyLnJiIjoKPgo+ICAgaGVhZGVyc19wYXRoID0g
RmlsZS5kaXJuYW1lKF9fRklMRV9fKSArICcvaGVhZGVycycKPiAgIERpci5jaGRpcihoZWFkZXJz
X3BhdGgpCj4gICBEaXJbIioucmIiXS5lYWNoIGRvIHxmaWxlfAo+ICAgICAgIHJlcXVpcmUgIiN7
aGVhZGVyc19wYXRofS8je2ZpbGV9Igo+ICAgZW5kCj4KPiAgSXMgaXQgZW5vdWdodCBvayBvciBj
YW4gaXQgcmVjZWl2ZSBzb21lIFJ1YnkncyBtYWdpYz8gWEQKPgo+ICBUaGFua3MgZm9yIGFueSBz
dWdnZXN0aW9uLgo+Cj4gIC0tCj4gIEnDsWFraSBCYXogQ2FzdGlsbG8KPiAgPGliY0BhbGlheC5u
ZXQ+Cj4K

Gennady Bystritsky

4/29/2008 5:08:00 PM

0

I=F1aki Baz Castillo wrote:
> Hi, is there any preferred way to load/require all the fiels
> in a directory?
> For example, my main file does:
>
> require "header.rb"
>
> and file "header.rb" must load all the related files contained in
> "headers/" directory. I do the following in "header.rb":
>
> headers_path =3D File.dirname(__FILE__) + '/headers'
> Dir.chdir(headers_path) Dir["*.rb"].each do |file|
> require "#{headers_path}/#{file}"
> end
>
> Is it enought ok or can it receive some Ruby's magic? XD
>
> Thanks for any suggestion.

You do use Ruby magic here ;-). The only thing I would suggest is that if y=
ou do not have to set the current directory to headers_path for some other =
reason, use this variation:

Dir[File.join(File.dirname(__FILE__), 'headers', "*.rb")].each do |file|
require file
end

Gennady.

ara.t.howard

4/29/2008 5:33:00 PM

0


On Apr 29, 2008, at 2:58 AM, I=F1aki Baz Castillo wrote:
> headers_path =3D File.dirname(__FILE__) + '/headers'
> Dir.chdir(headers_path)
> Dir["*.rb"].each do |file|
> require "#{headers_path}/#{file}"
> end
>
> Is it enought ok or can it receive some Ruby's magic? XD

glob =3D File.join( File.dirname(__FILE__), 'headers', '*.rb' )

Dir.glob(glob){|lib| require lib}

is a bit shorter...

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being =20
better. simply reflect on that.
h.h. the 14th dalai lama




Daniel Berger

4/29/2008 5:59:00 PM

0



On Apr 29, 11:33=A0am, "ara.t.howard" <ara.t.how...@gmail.com> wrote:
> On Apr 29, 2008, at 2:58 AM, I=F1aki Baz Castillo wrote:
>
> > headers_path =3D File.dirname(__FILE__) + '/headers'
> > =A0Dir.chdir(headers_path)
> > =A0Dir["*.rb"].each do |file|
> > =A0 =A0 =A0require "#{headers_path}/#{file}"
> > =A0end
>
> > Is it enought ok or can it receive some Ruby's magic? XD
>
> glob =3D File.join( File.dirname(__FILE__), 'headers', '*.rb' )
>
> Dir.glob(glob){|lib| require lib}
>
> is a bit shorter...

I seem to recall proposals for allowing something like "require header/
*" ala Java's import (maybe I brought it up myself?) but I can't
remember what the arguments against it were.

Dan

Iñaki Baz Castillo

4/29/2008 9:35:00 PM

0

El Martes, 29 de Abril de 2008, Gennady Bystritsky escribi=F3:

> You do use Ruby magic here ;-). The only thing I would suggest is that if
> you do not have to set the current directory to headers_path for some oth=
er
> reason, use this variation:
>
> Dir[File.join(File.dirname(__FILE__), 'headers', "*.rb")].each do |file|
> require file
> end

The problem is that I need to load before some specifi files because have=20
modules that others files require:

headers_path =3D File.dirname(__FILE__) + '/headers'
Dir.chdir(headers_path)

require "#{headers_path}/header.rb"
require "#{headers_path}/modules.rb"

Dir["hdr_*.rb"].each do |file|
require "#{headers_path}/#{file}"
end


But ok, thanks a lot for all your suggestions :)



=2D-=20
I=F1aki Baz Castillo

matt

5/3/2008 3:32:00 PM

0

Iñaki Baz Castillo <ibc@aliax.net> wrote:

> Hi, is there any preferred way to load/require all the fiels in a directory?
> For example, my main file does:
>
> require "header.rb"
>
> and file "header.rb" must load all the related files contained in
> "headers/" directory. I do the following in "header.rb":
>
> headers_path = File.dirname(__FILE__) + '/headers'
> Dir.chdir(headers_path)
> Dir["*.rb"].each do |file|
> require "#{headers_path}/#{file}"
> end
>
> Is it enought ok or can it receive some Ruby's magic?

If you say

$: << theDirectory

then you can just require the files by name, I believe... m.


--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...