我需要帮助…
我有一个这样的杂烩:

@ingredients = Hash.new
@ingredients[1] = "Biscottes Mini(recondo)"
@ingredients[2] = "Abadejo"
@ingredients[3] = "Acelga"
@ingredients[4] = "Agua de Coco"
@ingredients[5] = "Ajo"
@ingredients[6] = "Almidón de Arroz"
@ingredients[7] = "Anillos Con Avena Integral cheerios (nestle)"
@ingredients[8] = "Apio"

当我写“scotte”的时候,我需要搜索那个散列来找到“biscottes mini(recondo)”。
有什么帮助吗?
厚!

最佳答案

为什么在这里使用散列而不是数组您似乎不使用整数以外的键。
无论如何,此解决方案适用于数组和散列:

search_term = 'scotte'
# you could also use find_all instead of select
search_results = @ingredients.select { |key, val| val.include?(search_term) }
puts search_results.inspect

http://ruby-doc.org/core/classes/Enumerable.html#M001488

10-07 21:20