The Pug Automatic

Flickr-style tag splitting in Ruby

Written March 4, 2008. Tagged Ruby.

Someone asked on IRC for Ruby code to split tags Flickr style, e.g. getting the tags from tag1 tag2 "tag 3 has spaces" tag4.

I came up with this:

def parse_tags(string)
string.split(/"(.+?)"|\s+/).reject {|s| s.empty? }

It even preserves tag order, which you wouldn't get if you'd first gsub out (and store) quoted tags and then split the rest.