0

I have an array of links:

array = [link1, link2, link3, link4]
and array_of_hashes with two items: :names and :links

hash = { :names, :links } e.g.
array_of_hashes = [{ :names => name5, :links => link5}, {:names = name1, :links => link1}, ... ]

I want to do something with each pair of hashes (:names :links) from array_of_hashes which including links from the original array of links.

Thus, at final stage I need to find pair of hashes (in my case listed above):

{:names => name1, :links => link1}

cause link1 are listed in the array with links

UPD: Revised data... sorry for missunderstanding. Thanks a lot for your assistance.

3
  • 1
    Really unclear on what you're trying to achieve - are you attempting to filter the array_of_hashes based on whether the links value is present in array? Commented Aug 9, 2012 at 7:03
  • Yes, I'm trying to find names for each link in array from array_of_hashes. Commented Aug 9, 2012 at 7:09
  • So, you basically want a copy of array_of_hashes but containing only the links which are present in array? Commented Aug 9, 2012 at 7:16

1 Answer 1

2

If I understand your question correctly, this should do what you want:

# Cleaned up the setup a little
array_of_hashes = [
    {:names => 'name5', :links => 'link5'}, 
    {:names => 'name1', :links => 'link1'}, 
    {:names => 'name9', :links => 'link9'}]

array = ['link5', 'link1']

# This will filter the array
array_of_hashes.select{|x| array.include? x[:links]}

This gives the output => [{:names=>"name1", :links=>"link1"}]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.