Friday 15 January 2010

ruby - return an array of keys from hash when values match pattern -


I'm trying to run through the following hash

  my_family_pets_ages = {" Evi "= & gt; 6, "hobbi" => 3, "George" => 12, "Bogart" = & gt; 4, "Pali" = & gt; 4, "Annabelle" => 0, "move" => 3}  

and return an array of keys whose values ​​match a specific integer for ages. So, for example, if I wanted to find all the 3-year-old pets, it would simply return an array of their names.

  ["hoobe", "dito"]  

I have the following method, but I think I just have to return an array of keys Can not get the method, but I'm getting key = = value in an array like this:

["Hoobie" => 3, "Ditto" = & gt; 3]

Even the method that I have done so far

  def my_hash_finding_method (source, cheese_t_fund) source.select {| Name, age | Name if age == thing_to_find}  

Any hint? How do I return the keys

Use the bus, then to get an array matching Keys:

  def my_hash_finding_method (source, cheese_t_fund) source.select {| Name, age | Age == thing_to_find} .keys end  

See for more information.


No comments:

Post a Comment