From: Josh Cheek Date: 2013-01-04T14:08:52+09:00 Subject: Re: Require a ruby project to automatically include the modules in classes defined in the same .rb file --f46d04016989747cae04d26f65f0 Content-Type: text/plain; charset=ISO-8859-1 On Wed, Jan 2, 2013 at 9:58 PM, Marc Heiler wrote: > Hi. > > I would like to know how I could save one line of code. > > Situation right now: > > require 'evil_module' > > class Foo > include EvilModule > end > > Everyone knows this, this works fine. > > However, what I now want to do is, use only require, > and have this module included into ALL classes of > the .rb file. In other words: > > > require 'evil_module' > > class Foo > end > > Should become the same as the above. > > But only in .rb files from where the require is used. > > Is this possible? I was told it may be possible via > the required hook, but it would require changing > modules globally (monkey patch), which I absolutely > would not want to do. > > The reason why I need this, before anyone asks: > > I have a global colourizer method, which I use > for ALL my ruby projects and all my ruby classes, > but optionally. I.e. I want to toggle and control > whether to use ansicolours or not. > > The current code I have requires me to use an > explicit include, which is ok, but I'd rather > be able to eliminate one line of code, which > would in turn eliminate around 3000 lines of > code (the amount of classes I wrote in ruby so > far). > > -- > Posted via http://www.ruby-forum.com/. > > O.o You have 3000 classes? I can't imagine such a project. Anyway, there are ways you could do this, but they would require the blackest of magics (hooking into require or using set_trace_func, and then parsing the files in some manner to identify the namespaces around the classes so you can locate them to include the module). Really, what Robert suggested makes the most sense, include it into Object. But honestly, with a project that big, I think I'd want everything to be absolutely explicit all the time, anyway. 3000 is a huge number, though. Are you generating code? Why do all of your classes need to include a colourization module? I very rarely need any such thing, perhaps there is a deeper issue here which would be more useful to discuss than this idea (which is a highly questionable one anyway). --f46d04016989747cae04d26f65f0 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Wed, Jan 2, 2013 at 9:58 PM, Marc Heiler <lists@ruby-forum.com= > wrote:
Hi.

I would like to know how I could save one line of code.

Situation right now:

=A0 require 'evil_module'

=A0 class Foo
=A0 =A0 include EvilModule
=A0 end

Everyone knows this, this works fine.

However, what I now want to do is, use only require,
and have this module included into ALL classes of
the .rb file. In other words:


=A0 require 'evil_module'

=A0 class Foo
=A0 end

Should become the same as the above.

But only in .rb files from where the require is used.

Is this possible? I was told it may be possible via
the required hook, but it would require changing
modules globally (monkey patch), which I absolutely
would not want to do.

The reason why I need this, before anyone asks:

I have a global colourizer method, which I use
for ALL my ruby projects and all my ruby classes,
but optionally. I.e. I want to toggle and control
whether to use ansicolours or not.

The current code I have requires me to use an
explicit include, which is ok, but I'd rather
be able to eliminate one line of code, which
would in turn eliminate around 3000 lines of
code (the amount of classes I wrote in ruby so
far).

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


O.o You have 3000 classes? I can&= #39;t imagine such a project.

Anyway, there are wa= ys you could do this, but they would require the blackest of magics (hookin= g into require or using set_trace_func, and then parsing the files in some = manner to identify the namespaces around the classes so you can locate them= to include the module). Really, what Robert suggested makes the most sense= , include it into Object.

But honestly, with a project that big, I think I'd = want everything to be absolutely explicit all the time, anyway.
<= br>
3000 is a huge number, though. Are you generating code? Why d= o all of your classes need to include a colourization module? I very rarely= need any such thing, perhaps there is a deeper issue here which would be m= ore useful to discuss than this idea (which is a highly questionable one an= yway).
--f46d04016989747cae04d26f65f0--