Hello All, I have a little question on rails. For instance, if I need to provide additional method to the Object class (method that I could use in all the classes then), where can I put this code: class Object def my_new_method_1 ... end def my_new_method_2 ... end end I mean... can I add this in my_controlller.rb after the controller class definition ? Can I put it anywhere next to a class definition? Thanks a lot, Luc --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Luc wrote:> Hello All, > > I have a little question on rails. > For instance, if I need to provide additional method to the Object > class (method that I could use in all the classes then), where can I > put this code: > > class Object > def my_new_method_1 > ... > end > def my_new_method_2 > ... > end > end > > I mean... can I add this in my_controlller.rb after the controller > class definition ? Can I put it anywhere next to a class definition? > > Thanks a lot, > Luc > > > > > >I believe it''s not considered good practice to pollute the core ruby classes with your own methods unless you really need to. A better way would be to define these methods in a module (in lib) and then mix these into the models (i.e. include) that need them. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Yup module Doctor def write_prescription "Prescription written" end end module Musician def write_song "Song written" end end class Person include Doctor include Musician end p = Person.new p.write_prescription p.write_song That''s how ya do it! You can even do it at runtime. p = Person.new p.extend Doctor On 11/20/06, Chris T <ctmailinglists-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> > > Luc wrote: > > Hello All, > > > > I have a little question on rails. > > For instance, if I need to provide additional method to the Object > > class (method that I could use in all the classes then), where can I > > put this code: > > > > class Object > > def my_new_method_1 > > ... > > end > > def my_new_method_2 > > ... > > end > > end > > > > I mean... can I add this in my_controlller.rb after the controller > > class definition ? Can I put it anywhere next to a class definition? > > > > Thanks a lot, > > Luc > > > > > > > > > > > > I believe it''s not considered good practice to pollute the core ruby > classes with your own methods unless you really need to. A better way > would be to define these methods in a module (in lib) and then mix these > into the models (i.e. include) that need them. > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---