From: Joel Pearson Date: 2013-01-29T03:01:27+09:00 Subject: Re: Please explain in English I'll try to break it down, let us know if there's anything further that needs clarifying. #Declare a method with one argument def count_words(string) #Create an empty Hash (aka Dictionary) to modify it later res = Hash.new(0) #Convert the whole string to lowercase (returns a new object, doesn't modify in place) string.downcase #Use Regex to return each word ( "+" means until a non-word character) as an enumerator .scan(/\w+/) #Iterate through each of the words and return (map) a new object (which isn't used in this case) .map{|word| #Populate the hash on each iteration (overwriting existing values) res[word] = #Get the "size" of the array returned by searching the string for all instance of the current word string.downcase.scan(/\b#{word}\b/).size} #Explicitly return the hash ("return" isn't strictly required as this is the last line) return res end I can't helping feeling that there is a more efficient way to do this, given that the loop iterates needlessly multiple times over the duplicates. This does the same thing (not sure whether it's faster): def count_words(string) res = {} string.downcase! string.scan( /\w+/ ).uniq.each{ |word| res[word] = string.scan(/\b#{word}\b/).size } res end -- Posted via http://www.ruby-forum.com/.