0

I have an array @contacts of hashes, each of which has the following keys:

:full_name
:city
:state
:email

How would I iterate through the array to return the :city values uniquely? Below is my best attempt

@contacts.uniq {|hash| hash[:city]}
1
  • if this is rails, you could do it at the db layer... Commented Jan 27, 2013 at 5:08

1 Answer 1

2
@contacts.map { |k| k[:city] }.uniq
Sign up to request clarification or add additional context in comments.

2 Comments

This worked, thanks. Does this mean that any .method added to the block of code is executed when whatever conditions inside that block return a value/true?
No, it's equivalent to (t = @contacts.map { |k| k[:city] }; t.uniq)

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.