>
> Bonus points: if you can do the same thing for multiple words...back to
> the example, but search for "green AND teddy"...you'd get:
>
> ["Robert likes green beans, girls", "with moustaches, and teddy bears.
> John thinks"] as a result.
>

Is this what you want with multiple words?



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

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|
([0,y-n].max..[y+n,s-1].min)}.map{|z| f[z]}
end

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



# Output
#>   [["Robert", "likes", "green", "beans"], ["girls", "with",
"mustaches", "and", "teddy", "bears", "John"], ["with", "mustaches",
"and", "teddy", "bears", "John", "thinks"], ["mustaches", "and",
"teddy", "bears", "John", "thinks", "Robert"], ["bears", "John",
"thinks", "Robert", "is", "strange"], ["thinks", "Robert", "is",
"strange"]]




Harry