Skip to content

Instantly share code, notes, and snippets.

@marano
Created May 7, 2009 15:29
Show Gist options
  • Save marano/108160 to your computer and use it in GitHub Desktop.
Save marano/108160 to your computer and use it in GitHub Desktop.
A core string extension that Ive used many times.
class String
def canonical
mb_chars.strip.normalize(:kd).gsub(/[^\x00-\x7F]/n,'').downcase.to_s
end
def trim
gsub(/ /,'')
end
def self.random(len = 10)
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
newstring = ""
1.upto(len) { |i| newstring << chars[rand(chars.size-1)] }
return newstring
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment