Skip to content

Instantly share code, notes, and snippets.

@jin
Created September 30, 2011 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jin/1254195 to your computer and use it in GitHub Desktop.
Save jin/1254195 to your computer and use it in GitHub Desktop.
array = [
{:name=>"site a", :url=>"http://example.org/site/1/"},
{:name=>"site b", :url=>"http://example.org/site/2/"},
{:name=>"site c", :url=>"http://example.org/site/3/"},
{:name=>"site d", :url=>"http://example.org/site/1/"},
{:name=>"site e", :url=>"http://example.org/site/2/"},
{:name=>"site f", :url=>"http://example.org/site/6/"},
{:name=>"site g", :url=>"http://example.org/site/1/"}
]
array.inject([]) { |ar, it|
count_so_far = ar.count{|i| i[:url] == it[:url]}
it[:index] = count_so_far+1
ar << it
}
#=>
[
{:name=>"site a", :url=>"http://example.org/site/1/", :index=>1},
{:name=>"site b", :url=>"http://example.org/site/2/", :index=>1},
{:name=>"site c", :url=>"http://example.org/site/3/", :index=>1},
{:name=>"site d", :url=>"http://example.org/site/1/", :index=>2},
{:name=>"site e", :url=>"http://example.org/site/2/", :index=>2},
{:name=>"site f", :url=>"http://example.org/site/6/", :index=>1},
{:name=>"site g", :url=>"http://example.org/site/1/", :index=>3}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment