From: Sam Duncan Date: 2011-10-20T14:13:10+09:00 Subject: Re: why doesn't this work? On 20/10/11 17:36, Josh Cheek wrote: > 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 > Too true! Still not too bad =] def find_frequency(prose, word) prose.scan(/\b(#{Regexp.escape word})\b/im).length end Sam