Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- YourModel.find(:all, :group => "status")
- uniq_array = my_array.uniq_by {|obj| obj.id}
- class Array
- def uniq_by(&blk)
- transforms = []
- self.select do |el|
- should_keep = !transforms.include?(t=blk[el])
- transforms << t
- should_keep
- end
- end
- end
- class Array
- def uniq_by(&blk)
- transforms = {}
- select do |el|
- t = blk[el]
- should_keep = !transforms[t]
- transforms[t] = true
- should_keep
- end
- end
- end
- class Foo
- attr_accessor :foo, :bar, :baz
- def initialize(foo,bar,baz)
- @foo = foo
- @bar = bar
- @baz = baz
- end
- end
- objs = [Foo.new(1,2,3),Foo.new(1,2,3),Foo.new(2,3,4)]
- # find objects that are uniq with respect to attributes
- objs.inject([]) do |uniqs,obj|
- if uniqs.all? { |e| Marshal.dump(e) != Marshal.dump(obj) }
- uniqs << obj
- end
- uniqs
- end
- Hash[*recs.map{|ar| [ar[attr],ar]}.flatten].values
- objs.inject({}) {|h,e| h[e.attr]=e; h}.values
- h = {}
- objs.each {|e| h[e.attr]=e}
- h.values
- class A
- attr_accessor :val
- def initialize(v); self.val = v; end
- end
- objs = [1,2,6,3,7,7,8,2,8].map{|i| A.new(i)}
- objs.sort_by{|a| a.val}.inject([]) do |uniqs, a|
- uniqs << a if uniqs.empty? || a.val != uniqs.last.val
- uniqs
- end
Add Comment
Please, Sign In to add comment