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
 

转载于:https://www.cnblogs.com/or2-/p/6565337.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值