Hey guys, whats the best way to add a method to the array class so that it will act on an array? Can you give an example? -- 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 -~----------~----~----~----~------~----~------~--~---
On 10/19/06, Eric Gross <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hey guys, whats the best way to add a method to the array class so that > it will act on an array? Can you give an example? > > --Ruby classes are open, so you can simply do: class Array def sum inject(0) { |sum,v| sum+=v } end end You can also redefine existing methods, but you''ll have to be very careful with that. alias is your friend. Cheers, Max --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
where would that code snippet go? In the application controller? Max Muermann wrote:> On 10/19/06, Eric Gross <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> >> Hey guys, whats the best way to add a method to the array class so that >> it will act on an array? Can you give an example? >> >> -- > > Ruby classes are open, so you can simply do: > > class Array > def sum > inject(0) { |sum,v| sum+=v } > end > end > > You can also redefine existing methods, but you''ll have to be very > careful with that. alias is your friend. > > Cheers, > Max-- 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 -~----------~----~----~----~------~----~------~--~---
On 10/19/06, Eric Gross <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > where would that code snippet go? > > In the application controller?No. Create a new file under lib, for example array_extensions.rb and you should be fine. Max> > Max Muermann wrote: > > On 10/19/06, Eric Gross <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > >> > >> Hey guys, whats the best way to add a method to the array class so that > >> it will act on an array? Can you give an example? > >> > >> -- > > > > Ruby classes are open, so you can simply do: > > > > class Array > > def sum > > inject(0) { |sum,v| sum+=v } > > end > > end > > > > You can also redefine existing methods, but you''ll have to be very > > careful with that. alias is your friend. > > > > Cheers, > > Max > > > -- > 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 > -~----------~----~----~----~---~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---