uniq不会改变数组本身,而uniq!会直接修改数组,且uniq如果找到重复元素,则返回去重的数组,否则返回nil(这里比较容易理解错误而踩坑)

a = [5,2,2,5,3]a.uniq       # [5, 2, 3]
p a          # [5, 2, 2, 5, 3]a.uniq!      # [5, 2, 3]
p a          # [5, 2, 3]a.uniq!      # nil
p a          # [5, 2, 3]

参考:

https://stackoverflow.com/questions/33923492/ruby-what-is-the-difference-between-uniq-and-uniq

ruby中uniq和uniq!的区别相关推荐

  1. _.uniq_在Ruby中使用Array.compact和Array.uniq方法从Array中移除元素

    _.uniq Ruby Array.compact和Array.uniq方法 (Ruby Array.compact and Array.uniq Methods) In the last artic ...

  2. Ruby中的require_relative和require有什么区别?

    本文翻译自:What is the difference between require_relative and require in Ruby? Ruby中的require_relative和re ...

  3. ruby 集合 分组_在Ruby中找到两个集合之间的区别

    ruby 集合 分组 Finding differences simply means that finding elements that are uncommon between two sets ...

  4. 从Ruby中删除数组中的重复元素

    本文翻译自:Remove duplicate elements from array in Ruby I have a Ruby array which contains duplicate elem ...

  5. 如何在Ruby中使用数组方法

    介绍 (Introduction) Arrays let you represent lists of data in your programs. Once you have data in an ...

  6. Ruby中的设计模式

    继续 上 节讲述过的Singleton . Proxy 及 Iterator各模式,本节再来考察几个别的设计模式.下面按顺序来考察 Prototype . Template Method 和 Obse ...

  7. Ruby中的字符串与符号

    原文引用:http://blog.csdn.net/besfanfei/article/details/7966987 初学Ruby的时候,往往对字符串(String类)与符号(Symbol)这两种变 ...

  8. [2]rubyruby on rails入门笔记---Ruby中的异常

    Ruby中异常处理非常的重要,Ruby中异常处理,包括Exception 捕获,Retry,Raise,ensure ,Else格式,Throw...Catch已经类级别的异常.其具体格式和用法如下. ...

  9. ruby中的符号_Ruby中的凡人和不朽符号

    ruby中的符号 In this article, we're going to explore the following topics: 在本文中,我们将探讨以下主题: symbols are u ...

  10. ruby 中叹号问号的作用

    ruby中的方法可以以问号和叹号结尾,问号通常用于谓语方法,这种方法返回一个布尔值.例如array和hash类都定义了一个empty?方法,这个方法用于测试数据结构中有没有元素.         如果 ...

最新文章

  1. 【大数据技术干货】阿里云伏羲(fuxi)调度器FuxiMaster功能简介(一) 多租户(QuotaGroup)管理...
  2. pyrMeanShiftFiltering函数
  3. CV之YOLO:深度学习之计算机视觉神经网络tiny-yolo-5clessses训练自己的数据集全程记录
  4. 节假日api_iOS能跳过节假日的晚安闹钟捷径
  5. arraylist 线程安全_数据结构之Array、ArrayList、List、LinkedList对比分析
  6. Docker运行操作系统环境(BusyBoxAlpineDebian/UbuntuCentOS/Fedora)
  7. python 代码片段9
  8. AfxGetMainWnd( )函数
  9. sqlite3 交叉编译出现configure: error: C compiler cannot create executables
  10. CentOS 7服务器上使用Nginx+phpMyAdmin
  11. 02-JAVA语法基础
  12. 今年的移动Pwn2own破解大赛:iPhone+安卓=50万美元
  13. No such file or directory
  14. 安卓频谱仪audiotool中文_频谱分析仪 TEK2712
  15. DPDK ip分片与重组的设计实现
  16. 联想员工亲历联想大裁员:公司不是我的家
  17. 计算机如何连接隐藏的无线网络,无线网络隐藏了笔记本怎么连接?
  18. 好书分享、能量传递-《软技能 代码之外的生存指南》自我营销篇
  19. 前端加密 后端Java解密
  20. 基于决策树对鸢尾花进行分类

热门文章

  1. C#命名空间(namespace)学习
  2. ctfshow 新春欢乐赛
  3. 【gitee】本地建仓托管
  4. 如何从计算机截屏到U盘,win7电脑如何实现截图
  5. 路是一步一步走出来的
  6. 计算1到10亿的自然数包含多少个2
  7. NodeJS学习笔记 (15)二进制数据-buffer(ok)
  8. kafka 消费者组
  9. 解决:ValueError: (‘Unrecognized keyword arguments:‘, dict_keys([‘ragged‘]))
  10. Android5.0以上通知栏图标设计