Actually, I guess this makes a little more sense and is a little faster.


def my_get(str, num, substrings)
  f,g,n = str.delete(",.").split(/\s+/), substrings, num
  s = f.size
  (0...s).select{|x| g.include?(f[x])}.map{|y| f[([0,y-n].max..[y+n,s-1].min)]}
end


astring = "Robert likes green beans, girls with mustaches, and teddy
bears. John thinks Robert is strange."

p my_get(astring,3,["Robert","and","teddy","bears","strange"])




Harry