bigsmoke-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Nov-28 19:34 UTC
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bala Paranj
2006-Dec-03 05:33 UTC
Re: Plug-in question: define a module first or just open the class definition?
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. On 11/28/06, bigsmoke-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <bigsmoke-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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. > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
bigsmoke-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-05 17:49 UTC
Re: Plug-in question: define a module first or just open the class definition?
Well, I read that chapter before I started this thread :-) On Dec 3, 6:33 am, "Bala Paranj" <bcpar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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. > > On 11/28/06, bigsm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <bigsm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bala Paranj
2006-Dec-05 19:07 UTC
Re: Plug-in question: define a module first or just open the class definition?
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. On 12/5/06, bigsmoke-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <bigsmoke-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Well, I read that chapter before I started this thread :-) > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
bigsmoke-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-06 18:05 UTC
Re: Plug-in question: define a module first or just open the class definition?
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. On Dec 5, 8:07 pm, "Bala Paranj" <bcpar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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. > > On 12/5/06, bigsm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <bigsm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Well, I read that chapter before I started this thread :-)--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Alex Wayne
2006-Dec-06 18:58 UTC
Re: Plug-in question: define a module first or just open the
unknown wrote:> 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.The end result is more or less the same. However, think of mix-ins as a way to extend any class with a set of related functionality. In this respect, a module included into existing classes makes more semantic sense. If you adding a single utility method, simply opening the class would be fine. If you are adding a group of methods that work together toward a common functional goal, using a module is preferable since it really helps keep things organized and encapsulated. Also a module name can be auto-documenting. While the goal of a bunch of added method to a base class might be unclear, it makes it easier to understand if the name of the module being included is something descriptive, such as ActsAsPuppy or GoogleMapsFinder. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
bigsmoke-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-07 16:56 UTC
Re: Plug-in question: define a module first or just open the
On Dec 6, 7:58 pm, Alex Wayne <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> unknown wrote: > > 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. > > The end result is more or less the same. However, think of mix-ins as a > way to extend any class with a set of related functionality. In this > respect, a module included into existing classes makes more semantic > sense. > > If you adding a single utility method, simply opening the class would be > fine. If you are adding a group of methods that work together toward a > common functional goal, using a module is preferable since it really > helps keep things organized and encapsulated. > > Also a module name can be auto-documenting. While the goal of a bunch > of added method to a base class might be unclear, it makes it easier to > understand if the name of the module being included is something > descriptive, such as ActsAsPuppy or GoogleMapsFinder.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. :-D --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---