Ruby version of kmatch