Ruby, 2016-01-17 00:46:06 UTC
Today I am dealing with 'slug'.
Yeah, as I know slug term come from Wordpress. Slug mean that downcase character separated by dash. It's something like "i-am-slug". It usually use for URI permalink.
Let say I want to convert below string into slug;
@tstring = "Hi!, I am Slug"
To solve this;
1. Remove non alphanumeric
2. Remove any whitespace into single space.
3. Replace single white space with dash
4. Convert into downcase
Here is the code;
@tstring.gsub(/\W/,' ').gsub(/\s+/, ' ').gsub(/\s+/, '-').downcase # => hi-i-am-slug