采用哈希并返回数组的方法

时间:2016-02-22 00:01:14

标签: arrays methods hash iteration enumeration

我正在尝试构建一个带有两个参数的方法(source,在本例中是一个名称的哈希值,而thing_to_find,在这种情况下是一个age作为整数),并返回带有该整数的哈希键。输出应该是具有该值的散列键的数组(但不输出该值)。

我正在努力想要得到这个 - 非常感谢帮助。这是给出的......

my_family_pets_ages = {"Evi" => 6, "Ditto" => 3, "Hoobie" => 3, "George" => 12, "Bogart" => 4, "Poly" => 4, "Annabelle" => 0}


def my_hash_finding_method(source, thing_to_find)
# here i've tried all manner of has_value?, map, include? etc. - I think I'm probably having a problem with syntax...am I close with this?
   source.select { |k, v| v.include? thing_to_find }
end

输出应该是这样的     my_hash_finding_method(my_family_pets_ages,3)     #=>应该返回[“Hoobie”,“Ditto”]

0 个答案:

没有答案