Skip to content

Instantly share code, notes, and snippets.

@bsingr
Created September 15, 2010 10:34
Show Gist options
  • Select an option

  • Save bsingr/580536 to your computer and use it in GitHub Desktop.

Select an option

Save bsingr/580536 to your computer and use it in GitHub Desktop.
arr = [{:email => 1, :name => "foo"}, {:email => 1, :name => "bar"}, {:email => 2, :name => "foo"}]
uniqueness_by = :email
result = arr.inject({:collected => [], :indicators => []}) do |helper,item|
indicator = item[uniqueness_by] # indicates uniqueness
unless helper[:indicators].include?(indicator) # new indicator? then collect it!
helper[:indicators] << indicator # for later decisions
helper[:collected] << item # the actuall result to the user
end
helper
end
unique_arr = result[:collected] # [{:email => 1, :name => "foo"}, {:email => 2, :name => "foo"}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment