Skip to content

Instantly share code, notes, and snippets.

@workmad3
Forked from anonymous/gist:2032189
Created March 13, 2012 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save workmad3/2032221 to your computer and use it in GitHub Desktop.
Save workmad3/2032221 to your computer and use it in GitHub Desktop.
headlines = ["one","two","three","flee"]
#enumerables have a method #to_phrase.rhyme_key which gives a key based on pronunciation.
So headlines.map(&:to_phrase.rhyme_key) => 1,2,3,3 (for example)
#Now I want to filter out all the strings that don't have rhyme_key counterparts,
essentially leaving ["three","flee"] in the convoluted example above. I tried the code below,
but that just stores the rhyme_key values in the headlines array which is useless to me because
the original string literals get replaced with the values. How do I check against the values via
the methods without replacing the original strings?
headlines = headlines.map{|i| i.to_phrase.rhyme_key }.inject(Hash.new(0)){|i,c| i[c]+=1; i}.to_a.reject! {|b| b[1] < 2 }
rhyme_map = Hash.new {|h, k| h[k] = [] }
headlines.map{|h| h.to_phrase.rhyme_key}.zip(headlines).each {|(count, headline)| rhyme_map[count] << headline}
headlines_with_counterparts = rhyme_map.reject{|k, v| v.size <= 1 }.values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment