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

Posted by Marc Heiler (shevegen)
on 2013-01-03 04:58
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 by Ricky N. (ricky_n)
on 2013-01-03 05:16
(Received via mailing list)
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
Posted by Robert Klemme (robert_k78)
on 2013-01-03 22:45
(Received via mailing list)
On Thu, Jan 3, 2013 at 4:58 AM, Marc Heiler <lists@ruby-forum.com> 
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
Posted by tamouse mailing lists (Guest)
on 2013-01-04 03:50
(Received via mailing list)
On Wed, Jan 2, 2013 at 9:58 PM, Marc Heiler <lists@ruby-forum.com> 
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.
Posted by Josh Cheek (josh-cheek)
on 2013-01-04 06:09
(Received via mailing list)
On Wed, Jan 2, 2013 at 9:58 PM, Marc Heiler <lists@ruby-forum.com> 
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).
Posted by Robert Klemme (robert_k78)
on 2013-01-04 12:08
(Received via mailing list)
On Fri, Jan 4, 2013 at 6:08 AM, Josh Cheek <josh.cheek@gmail.com> 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
Posted by Peter Hickman (Guest)
on 2013-01-04 12:59
(Received via mailing list)
On 4 January 2013 11:07, Robert Klemme <shortcutter@googlemail.com> 
wrote:

> On Fri, Jan 4, 2013 at 6:08 AM, Josh Cheek <josh.cheek@gmail.com> 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?
Posted by tamouse mailing lists (Guest)
on 2013-01-05 03:06
(Received via mailing list)
On Fri, Jan 4, 2013 at 5:58 AM, Peter Hickman
<peterhickman386@googlemail.com> 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....
Posted by Robert Klemme (robert_k78)
on 2013-01-05 13:10
(Received via mailing list)
On Sat, Jan 5, 2013 at 2:05 AM, tamouse mailing lists
<tamouse.lists@gmail.com> 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
Posted by Josh Cheek (josh-cheek)
on 2013-01-05 15:33
(Received via mailing list)
On Sat, Jan 5, 2013 at 6:09 AM, Robert Klemme 
<shortcutter@googlemail.com>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.
Posted by tamouse mailing lists (Guest)
on 2013-01-05 18:35
(Received via mailing list)
On Sat, Jan 5, 2013 at 8:32 AM, Josh Cheek <josh.cheek@gmail.com> 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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.