Skip to content

Instantly share code, notes, and snippets.

@fronx
Created January 2, 2011 00:03
Show Gist options
  • Select an option

  • Save fronx/762128 to your computer and use it in GitHub Desktop.

Select an option

Save fronx/762128 to your computer and use it in GitHub Desktop.
Array.class_eval do
def uniq_by(&block)
self & Hash[
*inject([]) do |memo, item|
memo << block.call(item) << item
end
].values
end
end
puts ['c', 'a', ['Aa'], ['C', ''], 'b', 'A'].uniq_by { |s| s.to_s.downcase }.inspect
# => [["Aa"], ["C", ""], "b", "A"]
@fronx
Copy link
Author

fronx commented Jan 2, 2011

Something like [['C', ''], 'a', ['A'], ['c'], 'C', 'b', 'A'].uniq_by { |s| s.to_s.downcase } doesn't work yet (odd number of arguments for Hash).

@fronx
Copy link
Author

fronx commented Jan 2, 2011

And now it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment