mẫu đầu vào:Làm thế nào để nhận được tần số từ theo cách hiệu quả với ruby?
"I was 09809 home -- Yes! yes! You was"
và đầu ra:
{ 'yes' => 2, 'was' => 2, 'i' => 1, 'home' => 1, 'you' => 1 }
Mã của tôi mà không làm việc:
def get_words_f(myStr)
myStr=myStr.downcase.scan(/\w/).to_s;
h = Hash.new(0)
myStr.split.each do |w|
h[w] += 1
end
return h.to_a;
end
print get_words_f('I was 09809 home -- Yes! yes! You was');
rất liên quan: http://stackoverflow.com/questions/9480852/array-to-hash-words-count – tokland