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