ruby - Delete words that start with '#', except for the first n such words -


given string , integer n, want remove words starting "#" after nth occurrence.

for example, if n = 2 , string is:

"the mountains of #chamonix france famous skiing, alpine climbing, base-jumping, #paragliding, raw adventure , #home of first winter olympics. it's adventurers , photographers paradise. seen here, long exposure of #druis, 1 of many striking"

then result should be:

"the mountains of #chamonix france famous skiing, alpine climbing, base-jumping, #paragliding, raw adventure , of first winter olympics. it's adventurers , photographers paradise. seen here, long exposure of the, 1 of many striking"

the first 2 #-prefixed words, #chamonix , #paragliding, preserved, remaining #-prefixed words, #home , #druis, removed.

if want remove words starting "#" after nth occurrence, then:

string.gsub(/#\w+/).with_index(1){|s, i| > n ? "" : s} 

Comments