ruby中的== eql?和equal?区别
原文 http://www.wellho.net/mouth/985_Equality-in-Ruby-eql-and-equal-.html
Equality in Ruby - == eql? and equal?
The == comparison checks whether two values are equal
eql? checks if two values are equal and of the same type
equal? checks if two things are one and the same object.
How do I remember which is which ... The longer the operator, the more restrictive the test it performs
Example:
irb(main):013:0> val = 17
=> 17
irb(main):014:0> val == 17.0
=> true
irb(main):015:0> val.eql?(17.0)
=> false
irb(main):016:0> val.eql?(17)
=> true
三个等号的比较操作===
通常情况下这中方式与==是一样的,但是在某些特定情况下,===有特殊的含义:
在Range中===用于判断等号右边的对象是否包含于等号左边的Range;
正则表达式中用于判断一个字符串是否匹配模式,
Class定义===来判断一个对象是否为类的实例,
Symbol定义===来判断等号两边的符号对象是否相同。
(1..10) === 5 # true: 5属于range 1..10
/\d+/ === "123" # true: 字符串匹配这个模式
String === "s" # true: "s" 是一个字符串类的实例
:s === "s" # true
irb(main):017:0> val.equal?(17)
=> true
最新文章
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(22)-权限管理系统-模块导航制作
- <;<;<; Java提取网页源码
- 【LeetCode OJ】Linked List Cycle II
- three.js 简介
- CAF(C++ actor framework)使用随笔(延迟发送,消息转发,消息优先级)(四)
- android软键盘的用法总结
- Dungeon Master(poj 2251)
- ALTIUM 10 过孔设置开窗、不开窗
- SPOJ 220 Relevant Phrases of Annihilation(后缀数组+二分答案)
- Unity Debug类
- 在react.js上使用antd-design没有样式
- 我们必须要知道的RESTful服务最佳实践
- Mint-UI组件 MessageBox为prompt 添加判断条件
- java实现一个简单的Web服务器
- 钢琴培训班课程、课时及费用管理系统已提供ACM3.0新版下载
- C++(3):./Encryptor: undefined symbol:Z11startserviceLAKJDFLJALDKJFLLLLL
- 当SQL Server的实例位于集群的特定节点时,数据库无法远程访问
- 腾讯大数据之TDW计算引擎解析——Shuffle
- 弹窗中修改select默认值遇到的问题
- 从去除毛刺的策略看开运算opening_circle和闭运算closing_circle的异同