I have a method that counts the frequency of words in a string. I am manually including some words that should be deleted. What I've found is that for short strings, 'the' is removed...for longer strings such as the one below, the method still prints 'the'. Any ideas on why this is and how to fix it?
def count_words(string) words = string.downcase.split(' ') delete_list = ['the'] delete_list.each do |del| words.delete_at(words.index(del)) end frequency = Hash.new(0) words.each do |word| frequency[word.downcase] += 1 end return frequency.sort_by {|k,v| v}.reverse end puts count_words('Pros great benefits fair compensation reasonable time off Cons middle management are empty suits, void of vision and very little risk taking politics have gotten out of control since gates left the building.. sales metrics often do not reflect the contributions of the role, which demonstrates that line management is out of touch of what the individual contributors role really does middle management does not care about the career of his/her directs, 90% of the time management competes directly with their people, or takes credit for their work lots of back stabbing going on Microsoft changes the organization or commitment or comp model, faster than the average deal cycle, making it next to near impossible to develop momentum in role or a rhythm of success execs promote themselves in years when they freeze employees merit increases only way to advance is to step on your peers/colleagues and take credit for work you had no impact on, beat your chest loud enough and you get "visibility" you need to advance visibility is not based on performance by enlarge, it is based on being in your manager\'s swim lane for advancement I have observed people get promoted in years when they did not meet their quota, nor did the earn the highest performance on the team, they kissed their way to the promotion Advice to Senior Management 1, get back to risk taking and teaming, less politics please, you are killing the company 2, set realistic commitments and stick to them for multiple years, stop changing the game faster than your people can react 3, stop over engineering commitments and over segmenting the company, people are not willing to collaborate or be corporate citizens 4, too many empty suits in middle management, keep flattening out the company and getting rid of middle managers that run reports all day, get back to a culture where managers also sell and drives wins 5, keep your word microsoft, you said stability, but you keep tinkering with the org too much for any changes to take affect A great Culture Limitless opportunities Supportive Management team who are passionate about people A company that really does want you to have a good work life balance and backs it up with policies that enable you to manage how and where you work. Cons Support resources are constrained Can be overly competitve and hard to get noticed Sales rewards are definitely prioritised and marketing cuts are always prioritised. Consumer organisation is still far from ideal. Advice to Senior Management Focus on getting the internal organisation simplified to improve performance and increase empowerment. Get some REAL consumer focus and invest for the long term Start connecting with people, focussing on telling stories rather than selling products.')
frequency[word.downcase]as words are already downcased. You should also useeach_with_objectinstead of yourwords.eachloop. See my answer.delete_at(words.index(del)), it just deletes the first occurrence. To fix it, do what @meagar said.