but it appears that (i.to_s.downcase) is always different from
(word.downcase), even if the words are the same. can someone explain it
to me, please?
The problem is the way you store the split sentence. The return value of
split is an array and you are storing that array into the empty array,
so
inside the delete_if loop you are dealing with the whole array of words,
not
individual words. Now calling to_s on an array will return a string that
contains all the elements from that array, concatenated. Obviously this
string will not match any single word.
s = “This is a test of this”
=> “This is a test of this”
On Wed, Oct 19, 2011 at 10:44:37PM +0900, Ammar A. wrote:
By the way, you don’t need to call Array.new to create a new empty array.
The following is equivalent:
my_array = []
I prefer Array.new for that usage, because (in my opinion) it more
clearly communicates the intent of creating a new array waiting to be
filled. I prefer to avoid empty array literals for most purposes. Your
mileage may vary, I suppose.
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