Hey guys this should be an easy one, how do i define a method that i can reuse for strings. eg/ "mystring".sanitize i know how to write the regex for such a method but where would i put it ??? cheers Adam --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 4/02/2008, at 11:01 PM, Adam Jones wrote:> > Hey guys this should be an easy one, > how do i define a method that i can reuse for strings. > > eg/ "mystring".sanitize > > i know how to write the regex for such a method but where would i put > it ??? > > cheers Adam > > >i think i may have answered it ...... String.class_eval do # define new method def sanitize self.gsub(/[^[:alnum:]]+/, ''_'') end end i dumed that into my app controller and its working --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
class String def sanitize self.gsub(/[^[:alnum:]]+/, ''_'') end end end Is better looking On Feb 4, 2008 8:33 PM, Adam Jones <adam-K36Kqf6HJK7JKwlM9GxbOw@public.gmane.org> wrote:> > > On 4/02/2008, at 11:01 PM, Adam Jones wrote: > > > > > Hey guys this should be an easy one, > > how do i define a method that i can reuse for strings. > > > > eg/ "mystring".sanitize > > > > i know how to write the regex for such a method but where would i put > > it ??? > > > > cheers Adam > > > > > > > > i think i may have answered it ...... > > String.class_eval do > # define new method > def sanitize > self.gsub(/[^[:alnum:]]+/, ''_'') > end > end > > i dumed that into my app controller and its working > > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 4/02/2008, at 11:35 PM, Ryan Bigg wrote:> class String > def sanitize > self.gsub(/[^[:alnum:]]+/, ''_'') > end > end > end > > Is better looking > > On Feb 4, 2008 8:33 PM, Adam Jones <adam-K36Kqf6HJK7JKwlM9GxbOw@public.gmane.org> wrote: > > > On 4/02/2008, at 11:01 PM, Adam Jones wrote: > > > > > Hey guys this should be an easy one, > > how do i define a method that i can reuse for strings. > > > > eg/ "mystring".sanitize > > > > i know how to write the regex for such a method but where would i > put > > it ??? > > > > cheers Adam > > > > > > > > i think i may have answered it ...... > > String.class_eval do > # define new method > def sanitize > self.gsub(/[^[:alnum:]]+/, ''_'') > end > end > > i dumed that into my app controller and its working > > > > > > -- > Ryan Bigg > http://www.frozenplague.net > Feel free to add me to MSN and/or GTalk as this email. > >Thanks. Where is the ideal place to put that?? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I have a file in my lib called custom_methods.rb, I load it by doing require ''custom_methods'' in my environment file, I put all my class additions in there. On Feb 4, 2008 9:09 PM, Adam Jones <adam-K36Kqf6HJK7JKwlM9GxbOw@public.gmane.org> wrote:> > On 4/02/2008, at 11:35 PM, Ryan Bigg wrote: > > class String > def sanitize > self.gsub(/[^[:alnum:]]+/, ''_'') > end > end > end > > Is better looking > > On Feb 4, 2008 8:33 PM, Adam Jones <adam-K36Kqf6HJK7JKwlM9GxbOw@public.gmane.org> wrote: > > > > > > > On 4/02/2008, at 11:01 PM, Adam Jones wrote: > > > > > > > > Hey guys this should be an easy one, > > > how do i define a method that i can reuse for strings. > > > > > > eg/ "mystring".sanitize > > > > > > i know how to write the regex for such a method but where would i put > > > it ??? > > > > > > cheers Adam > > > > > > > > > > > > > i think i may have answered it ...... > > > > String.class_eval do > > # define new method > > def sanitize > > self.gsub(/[^[:alnum:]]+/, ''_'') > > end > > end > > > > i dumed that into my app controller and its working > > > > > > > > > -- > Ryan Bigg > http://www.frozenplague.net > Feel free to add me to MSN and/or GTalk as this email. > > > > Thanks. > Where is the ideal place to put that?? > > > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---