question about modules

J

James French

Hi group,

I have a main.rb file which is the entry point for a system. It loads a lot=
of other ruby files using 'require'. I would like to put this whole system=
inside a module 'X' basically for namespacing purposes.

My question is: is it possible to just place the whole of main.rb in module=
X and then expect all classes and methods etc that get created via require=
to be automatically imported into that module (ie just specify the module =
once in main.rb)? It would be great if this was possible as I wouldn't have=
to wrap every single file in module X statements (and wouldn't that create=
a nested module X inside the outer module X??)

Any help much appreciated,
James
 
R

Robert Klemme

Hi group,

I have a main.rb file which is the entry point for a system. It loads a lot of other ruby files using 'require'. I would like to put this whole system inside a module 'X' basically for namespacing purposes.

My question is: is it possible to just place the whole of main.rb in module X and then expect all classes and methods etc that get created via require to be automatically imported into that module (ie just specify the module once in main.rb)?
No.

It would be great if this was possible as I wouldn't have to wrap every single file in module X statements (and wouldn't that create a nested module X inside the outer module X??)

No. You have to explicitly nest every individual file unless you want
to start using eval or other tricks to save the work of typing.

Actually for the typing you may be able to do something like this (untested):

find lib/x -type f -name \*.rb -print0 | xargs -r0 -n 1 ruby -p '-i~'
-e 'BEGIN { puts "module X" }; END { puts "end"}'

Kind regards

robert
 
J

James French

-----Original Message-----
From: Robert Klemme [mailto:[email protected]]=20
Sent: 17 February 2011 12:24
To: ruby-talk ML
Subject: Re: question about modules

Hi group,

I have a main.rb file which is the entry point for a system. It loads a l=
ot of other ruby files using 'require'. I would like to put this whole syst=
em inside a module 'X' basically for namespacing purposes.
My question is: is it possible to just place the whole of main.rb in modu=
le X and then expect all classes and methods etc that get created via requi=
re to be automatically imported into that module (ie just specify the modul=
e once in main.rb)?

No.
It would be great if this was possible as I wouldn't have to wrap every s=
ingle file in module X statements (and wouldn't that create a nested module=
X inside the outer module X??)

No. You have to explicitly nest every individual file unless you want
to start using eval or other tricks to save the work of typing.

Actually for the typing you may be able to do something like this (untested=
):

find lib/x -type f -name \*.rb -print0 | xargs -r0 -n 1 ruby -p '-i~'
-e 'BEGIN { puts "module X" }; END { puts "end"}'

Kind regards

robert

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

OK. Thank Robert - much appreciated.
 

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,582
Members
45,068
Latest member
MakersCBDIngredients

Latest Threads

Top