Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

String#to_id (See related posts)

Convert any (English) text to a programming-language friendly identifier.

class String
  def to_id
    downcase.gsub(/\W+/, ' ').strip.gsub(' ', '_')
  end
 
  def to_id!
    replace to_id
  end
end

Comments on this post

peter posts on Apr 05, 2006 at 03:24
One minor problem might be if the first character was a digit, as those can often have problems with identifiers.

You need to create an account or log in to post comments to this site.


Click here to browse all 4858 code snippets

Related Posts