Require a ruby project to automatically include the modules in classes defined in the same .rb file

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).

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 Thu, Jan 3, 2013 at 4:58 AM, Marc H. [email protected]
wrote:

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

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:

Is this possible?

Is it desirable? Wouldn’t the code be significantly harder to read?

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.

What does that mean? I mean what does this have to do with the module
or the method?

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).

If that method is so globally used you could include it in Object.

Kind regards

robert

On Wed, Jan 2, 2013 at 9:58 PM, Marc H. [email protected]
wrote:

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.

Are you telling us that every class you’ve written writes output? This
seems rather odd – better to localise that to a specific class which
you can switch colouring styles on and off with, rather than make
every class do it’s own colourised output. Think of Logger – you can
tell it to use a different formatter, and you can easily construct a
global Logger object that writes where you wish. Then in your various
classes, you call the Logger object and leave it to handle the
formatting and writing.

On Fri, Jan 4, 2013 at 6:08 AM, Josh C. [email protected] wrote:

O.o You have 3000 classes? I can’t imagine such a project.

That’s not overly large for a reasonable sized project I’d say.

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.

Or, even better, make it a “function”, i.e. define it at top level and
pass the object as argument.

But honestly, with a project that big, I think I’d want everything to be
absolutely explicit all the time, anyway.

+1

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).

Yes, that might well be.

Kind regards

robert

On Wed, Jan 2, 2013 at 9:58 PM, Marc H. [email protected]
wrote:

end
class Foo

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).

On Fri, Jan 4, 2013 at 5:58 AM, Peter H.
[email protected] wrote:

Looking over all the projects in our repository the number of classes (from
the rake stats command) most projects have less than 100 classes, a few make
it into the low 100s and two make it into the 1000s all due to the fact that
someone included an external project (soap4r and the like) in the lib
folder.

Are people routinely creating 1000s of classes in a project?

it begins to smell like Java…

On Sat, Jan 5, 2013 at 2:05 AM, tamouse mailing lists
[email protected] wrote:

it begins to smell like Java…
Hehe. Actually I had Java projects in mind. I don’t really have
numbers how many classes similar projects in Java and Ruby actually
have but I would assume the Ruby project to have less classes. How
much less might depend on the problem domain. Generally the order of
magnitude (i.e. a few thousand classes) does not sound too unrealistic
for a reasonably sized project which has passed through a few
development cycles.

Cheers

robert

On Sat, Jan 5, 2013 at 6:09 AM, Robert K.
[email protected]wrote:

O.o You have 3000 classes? I can’t imagine such a project.
it into the low 100s and two make it into the 1000s all due to the fact
have but I would assume the Ruby project to have less classes. How
much less might depend on the problem domain. Generally the order of
magnitude (i.e. a few thousand classes) does not sound too unrealistic
for a reasonably sized project which has passed through a few
development cycles.

Cheers

robert

Hmm, going to the root directory for my current work project, which has
had
a team of 5-8 people on it for about a year and a half,

$ find . -type file -name .rb | xargs ruby -E iso-8859-1 -ne 'print if
/^[^#]
((\b|\s)class\b\s[A-Z])|Class.new|Struct.new/’ | sort -u | wc
-l
1378

But that is across all our services, gems that we wrote, test code,
migrations, etc.

On Sat, Jan 5, 2013 at 8:32 AM, Josh C. [email protected] wrote:

find . -type file -name .rb | xargs ruby -E iso-8859-1 -ne 'print if
/^[^#]
((\b|\s)class\b\s[A-Z])|Class.new|Struct.new/’ | sort -u | wc -l

10-15 people developing a year and a half, approximately: 1198
Checking similar, but restricted to just app/ and lib/ (leaving out
migrations and tests): 434

On 4 January 2013 11:07, Robert K. [email protected]
wrote:

On Fri, Jan 4, 2013 at 6:08 AM, Josh C. [email protected] wrote:

O.o You have 3000 classes? I can’t imagine such a project.

That’s not overly large for a reasonable sized project I’d say.

Seriously?

Looking over all the projects in our repository the number of classes
(from
the rake stats command) most projects have less than 100 classes, a few
make it into the low 100s and two make it into the 1000s all due to the
fact that someone included an external project (soap4r and the like) in
the
lib folder.

Are people routinely creating 1000s of classes in a project?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs