Plug-in question: define a module first or just open the class definition?

I notice that some plug-ins first do something like:   module MyPlugin     def do_something_very_funky     end   end

and than include this into one of Rails' existing classes (often in the same file).

  module ActionController     class Base        include MyPlugin     end   end

Is there any advantage to this compared to simply doing:

  module ActionController     class Base       def so_something_very_funky       end     end   end

I can't think of any myself.

Read the chapter on Classes and Modules in Programming Ruby by Dave Thomas. Then read the Rails Plugin - Extending the Rails Core Functionality by James Adam for an indepth coverage on plugins.

Well, I read that chapter before I started this thread :slight_smile:

I don’t think you can use the same name ActionController for your module. The main idea here is that you once you define a module with your methods in it, you can mix-in any of the classes that you wish.

It gives you a multiple-inheritance like behaviour. It provides namespace also.

I understand which purpose modules serve. My question, however, was if there's any advantage to defining a module for no other purpose than extending a particular class instead of just directly changing that class. I hope I didn't add more confusion to my already vague question.

Thank your very much for your clear description of the pros and cons of each approach. This was a much more detailed explanation than I could have ever hoped for. :smiley: