Skip to content

Instantly share code, notes, and snippets.

@lachie
Forked from benaskins/gist:34631
Created December 11, 2008 07:20
Show Gist options
  • Save lachie/34640 to your computer and use it in GitHub Desktop.
Save lachie/34640 to your computer and use it in GitHub Desktop.
class Foo
attr_accessor :some_attr
def initialize(val)
self.some_attr = val
end
def ==(other)
self.some_attr == other.some_attr
end
end
x = Foo.new(1)
y = Foo.new(1)
z = Foo.new(2)
[x,y,z].uniq # => [x,y,z] :(
class Array
def buniq(&block)
uniq = {}
each {|e| uniq[yield(e)] = e}
uniq.values
end
end
[x,y,z].buniq {|o| o.some_attr} #=> y,z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment