From: Michal Zacik Date: 2011-10-19T22:35:42+09:00 Subject: Re: why doesn't this work? On 10/19/2011 03:09 PM, raphael teller wrote: > hey, I am a C programmer that decided to learn Ruby. I got some > exercises to do, but I'm having some trouble in this (silly)one: > > Problem Statement > Given a sentence containing multiple words, find the frequency of a > given word in that sentence. > > i wrote the code below > > def find_frequency(sentence, word) > array = Array.new > array<< sentence.split(/ /) > array.delete_if {|i| i.to_s.downcase != word.downcase}.length > end > > 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? > > thanks > Hi, array<< sentence.split(/ /) # this pushes array into array array = sentence.split(/ /) and i.to_s.downcase # to_s isn't needed def find_frequency(sentence, word) array = Array.new array = sentence.split(/ /) array.delete_if {|i| i.downcase != word.downcase}.length end Michal