From: Sam Duncan Date: 2011-10-20T13:29:59+09:00 Subject: Re: why doesn't this work? On 20/10/11 16:48, botp wrote: > On Thu, Oct 20, 2011 at 6:23 AM, Gunther Diemant wrote: >> def find_frequency(sentence, word) >> sentence.split(/ /).select { |w| w.downcase == word.downcase }.length >> end >> or maybe even better: >> def find_frequency(sentence, word) >> sentence.split(/ /).map { |w| w.downcase }.count(word.downcase) >> end > count does accept a block, so > > def find_frequency(sentence, word) > word=word.downcase > sentence.split.count{ |w| w.downcase == word} > end Just for fun ruby-1.9.2-p290 :001 > lorem = "On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains." => "On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains." 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 => nil ruby-1.9.2-p290 :005 > find_frequency(lorem, "selection") => 1 ruby-1.9.2-p290 :006 > find_frequency(lorem, "and") => 10 ruby-1.9.2-p290 :007 > find_frequency(lorem, "frequently") => 1 ruby-1.9.2-p290 :008 > find_frequency(lorem, "that") => 3 Sam