From: Ricky Ng Date: 2013-01-03T13:16:04+09:00 Subject: Re: Require a ruby project to automatically include the modules in classes defined in the same .rb file --e89a8f839dbff3322104d25a9e57 Content-Type: text/plain; charset=ISO-8859-1 Just random theorizing, but couldn't you, in the "required" hook access __DATA__ to see all the classes defined in the current file. Then take the ClassName.include(self.to_s). Whether this is a good idea... I'm not sure =D On Wed, Jan 2, 2013 at 7: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/. > > -- Incoherently, Ricky Ng --e89a8f839dbff3322104d25a9e57 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Just random theorizing, but couldn't you, in the "= ;required" hook access __DATA__ to see all the classes defined in the = current file. Then take the ClassName.include(self.to_s).

Whether this is a good idea... I'm not sure =3DD


On Wed, Jan 2, 2013 at = 7: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/.




-- Incoherently,
Ricky Ng
--e89a8f839dbff3322104d25a9e57--