1
a[0] = {:id => '1234', :value => '37'}
a[1] = {:id => '4321', :value => '50'}
a[2] = {:id => '1122', :value => '50'}

From here I want to be able to check to see if a hash exists with :id => '4321' without having to loop through the array manually. Is there anything where I can do something like this: a.exists?(:id => '4321') ? I've tried a few things but can't seem to figure it out. Thanks!

1 Answer 1

6

How about:

a.any? {|x| x[:id] == '4321' }

That will return true if the block returns true.

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

1 Comment

Similar to stackoverflow.com/questions/3794039/… - but a different operation that I needed, thanks!

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.