在ruby中使用一个数组搜索第二个数组的频率

在ruby中使用一个数组搜索第二个数组的频率,ruby,arrays,Ruby,Arrays,我有一个数组-1 arr1 =['s','a','sd','few','asdw','a','sdfeg'] 和第二个数组 arr2 = ['s','a','d','f','w'] 我想取arr1,通过输入arr2和结果对字母的频率进行排序 [s=> 4, a=> 2, d => 3] So on and so forth. 就我所能应付的范围而言。。下面什么都不管用,只是我的想法 hashy = Hash.new print "give me a sentan

我有一个数组-1

 arr1 =['s','a','sd','few','asdw','a','sdfeg']
和第二个数组

 arr2 = ['s','a','d','f','w']
我想取arr1,通过输入arr2和结果对字母的频率进行排序

[s=> 4, a=> 2, d => 3] So on and so forth.
就我所能应付的范围而言。。下面什么都不管用,只是我的想法

 hashy = Hash.new

 print "give me a sentance "
 sentance = gets.chomp.downcase.delete!(' ') 
 bing = sentance.split(//)
 #how = sentance.gsub!(/[^a-z)]/, "") #Remove nil result
 #chop = how.to_s.split(//).uniq
 #hashy << bing.each{|e|  how[e] }
 #puts how.any? {|e| bing.count(e)}
 #puts how, chop

 bing.each {|v| hashy.store(v, hashy[v]+1 )}
 puts bing
hashy=Hash.new
打印“给我一句话”
sentance=get.chomp.downcase.delete!(' ') 
bing=sentance.split(/)
#how=sentance.gsub!(/[^a-z)]/,“”)#删除零结果
#chop=如何分割(//).uniq

#hashy我假设您要计算输入的句子中的所有字母,而不是数组1。假设如此,我的看法如下:

hashy = Hash.new()

['s','a','d','f','w'].each {|item| hashy[item.to_sym] = 0}

puts "give me a sentence"

sentence = gets.chomp.downcase.delete!(' ')
sentence_array = []
sentence.each_char do |l|
    sentence_array.push(l)
end

hashy.each do |key, value|
    puts "this is key: #{key} and value #{hashy[key]}"
    sentence_array.each do |letter|
        puts "letter: #{letter}"
        if letter.to_sym == key
            puts "letter #{letter} equals key #{key}"
            value = value + 1
            hashy[key] = value
            puts "value is now #{value}"
        end
    end
end

puts hashy
“输入数组2对字母频率排序”是什么意思?