sanitizing strings

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

class String def sanitize self.gsub(/[[1]]+/, ‘_’) end end end

Is better looking


  1. :alnum: ↩︎

Thanks.

Where is the ideal place to put that??

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.