Rails: uniq vs. distinct

前端 未结 4 1526
时光取名叫无心
时光取名叫无心 2020-12-10 10:02

Can someone briefly explain to me the difference in use between the methods uniq and distinct?

I\'ve seen both used in similar context, but

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-10 10:43

    Its not exactly answer your question, but what I know is:

    If we consider ActiveRecord context then uniq is just an alias for distinct. And both work as removing duplicates on query result set(which you can say up to one level).

    And at array context uniq is so powerful that it removes duplicates even if the elements are nested. for example

    arr = [["first"], ["second"], ["first"]]
    

    and if we do

    arr.uniq
    

    answer will be : [["first"], ["second"]]

    So even if elements are blocks it will go in deep and removes duplicates.

    Hope it helps you in some ways.

提交回复
热议问题