This opportunity was trying to write a regular expression and we’ve probably write a better … what this function is to process a string that is word for word count every word in a block…
#!/usr/bin/ruby
#Extract each word found in a block of code
class String
def word_count
frecuencies = Hash.new(0)
downcase.scan(/\w+/) { |word| frecuencies[word] += 1 }
return frecuencies
end
end
Then you can loading under console prompt and testing funtion
>> load 'word_count.rb'
=> true
>> %{Tinix tinix tinix tinivella Tinivella}.word_count
=> {"tinivella"=>2, "tinix"=>3}
>>
that is all.....Tinix

Post a Comment