Guest User

Untitled

a guest
Feb 21st, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.39 KB | None | 0 0
  1. class Foo
  2. attr_accessor :some_attr
  3. def initialize(val)
  4. self.some_attr = val
  5. end
  6.  
  7. def ==(other)
  8. self.some_attr == other.some_attr
  9. end
  10. end
  11.  
  12. x = Foo.new(1)
  13. y = Foo.new(1)
  14. z = Foo.new(2)
  15.  
  16. [x,y,z].uniq # => [x,y,z] :(
  17.  
  18. class Array
  19. def buniq(&block)
  20. uniq = {}
  21. each {|e| uniq[yield(e)] = e}
  22. uniq.values
  23. end
  24. end
  25.  
  26. [x,y,z].buniq {|o| o.some_attr} #=> y,z
Add Comment
Please, Sign In to add comment