Last Updated: February 25, 2016
·
550
· pahnin

If you have to iterate over bunch of objects and make a uniq array of values from each object

Say you have an array of objects you need to call a method on each object and you need to make an uniq array of these values
You can use something like

values_a = []
list.each do |l|
  values_a << l unless values_a.include?(l)
end

but using uniq in this case is better than using include? times n is

values_a = []
list.each do |l|
  values_a << l 
end
val`ues_a = values_a.uniq

unless you are dealing with HUGE number of records this should work better.

you can also use sets Set#add but its much worse with the performace