From: Josh Cheek Date: 2011-10-20T13:36:52+09:00 Subject: Re: why doesn't this work? --0022159f018ede1bc104afb37eb9 Content-Type: text/plain; charset=ISO-8859-1 On Wed, Oct 19, 2011 at 11:29 PM, Sam Duncan wrote: > > ruby-1.9.2-p290 :002 > def find_frequency(prose, word) > ruby-1.9.2-p290 :003?> prose.scan(/\b(#{word})\b/im).**length > ruby-1.9.2-p290 :004?> end > > This was my approach, too. But by the time you make it robust, it's no longer elegant. i.e. it should really be escaped, leading to: def find_frequency(prose, word) regex = Regexp.new "\\b#{Regexp.escape word}\\b", Regexp::IGNORECASE prose.scan(regex).length end --0022159f018ede1bc104afb37eb9--