打印

== || eql? || equal?

本主题由 xavier 于 2008-3-21 19:30 解除置顶

== || eql? || equal?

这是我在几月前发现一个比较有趣的地方,查看下列代码:

#!/usr/bin/env ruby
#
#  Created by Yudi Xue on 2007-08-05.
#  Copyright (c) 2007. All rights reserved.


a = {'first' => 'girl', 'second' => 'appealing'}
b = {'first' => 'girl', 'second' => 'appealing'}

if a == b
  puts("yes, a and b are identical by ==")
else
  puts("no, the first test on == is failed")
end

if a.eql?(b) 
  puts("and yes, a and b are identical by eql") 
else 
  puts("no, the second test on eql? method is failed")
end

if a.equal?(b)
  puts("a and b have the same obj id")
else
  puts("a and b have different obj id")
end


输出:

Desktop $chmod 755 test.rb 
Desktop $./test.rb 
yes, a and b are identical by ==
no, the second test on eql? method is failed
a and b have different obj id
Desktop $


再看看下列ruby console代码:

Desktop $irb
irb(main):001:0> a = "girl"
=> "girl"
irb(main):002:0> b = "girl"
=> "girl"
irb(main):003:0> a.object_id
=> 276820
irb(main):004:0> b.object_id
=> 270550
irb(main):005:0> a.eql?(b)
=> true
irb(main):006:0> a == b
=> true
irb(main):007:0> a.equal?(b)
=> false
irb(main):008:0> 



ruby是面向对象的编程语言(虽然有许多人喜欢叫ruby为脚本语言)
在ruby中存在多种比较对象的几个function不是很明晰,容易令人混淆。
我们熟悉的 == 用来比较存在对象中的值
eql? 有些奇怪,是相对于 == 来讲更加严格的对比,从上面例子可以看到 hash.eql? 的结果为 false。
equal? 则用来对比对象的id。

更多细节可以在 the ruby way 第二版402页看到


[ 本帖最后由 yudi 于 2007-8-6 04:26 编辑 ]
本帖最近评分记录
  • blackanger R币 +3 2007-8-7 11:06
  • drive2me R币 +2 仔细,需要这样的CASE。 2007-8-6 20:04
  • skyover R币 +4 精品文章 2007-8-6 09:47

TOP

感谢分享如此好的文章,不然我还一直以为equal?是eql?的另一个名称呢。呵呵。

我想以我为代表的“Ruby新手“就需要这样的小CASE文章才指点一下。谢谢您,Yudi。
谢谢大家加入Ruby中文社区!
[寻找您身边的Rubyist.]

TOP

这个。。。 skyover太谦虚了
其实我才是新新手。。。 很感谢在这个时候提供一个如此好的交流平台,让我不至于有话没处说太郁闷
这个礼拜忙着弄 yudionrails.com, 改天把发布的经验整理一下放上来。

TOP

刚去看了你的Blog,看起来还不错,就是没写完吧。呵呵。

现在是不是很流行xxx on rails这样的域名?哈哈。我也去注册一个。
谢谢大家加入Ruby中文社区!
[寻找您身边的Rubyist.]

TOP

还行,连我的名字念起来还挺上口。 还没写完,差老远呢

TOP

别都跑了呀,我们这里有个人空间的。
哥几个,回来!哈哈!

我说,Yudi闷着干吗呢?原来呀...开小灶呢...
Flying Piggy...! 
天地人合一!

TOP

感觉和java的用法正好相反,java中equal方法是用来笔记对象的值,==比较的是对象的引用地址,也可以说是object_id

Ruby中,eql?方法和==方法比较的是对象的值,而equal?则是比较对象的引用地址。。。我还以为eql?是equal?的缩写方法呢,原来有这样的区别,感谢分享。。。

I.forget('you'){|something| something.remember.deepen}

TOP

………………

这个programming ruby上面刚开始没多久的时候就有说的,==是判断是值,相当于java里面的equal;eql?()判断的是值以及类型;equal?()判断的是地址。

a = 5
b = 5.0
puts a==b
puts a.eql?(b)


TOP


==       Test for equal value.

===      Used to compare each of the items with the target in the when clause of a case statement.

<=>      General comparison operator. Returns −1, 0, or +1, depending on
        whether its receiver is less than, equal to, or greater than its argument.

<, <=, >=, > Comparison operators for less than, less than or equal, greater than or
        equal, and greater than.

=~       Regular expression pattern match.

eql?      True if the receiver and argument have both the same type and equal
        values. 1 == 1.0 returns true, but 1.eql?(1.0) is false.

equal?     True if the receiver and argument have the same object ID.

TOP

结论:equal? 就是 C# 里的 Object.ReferenceEquals方法,确认是否引用相等。
谢谢大家加入Ruby中文社区!
[寻找您身边的Rubyist.]

TOP

奇怪:

arr = %w[a b c a a]
tmp = arr[0]
tmp.eql?("arr[4]")
=>false


eql?不是比较值和类型的吗??
这里,tmp和arr[4]的值都是a,类型都是String,为什么是false?
当然用==来比较是没问题的

真是迷糊了。。。
tmp.eql?(arr[4])
应该是这样。。。错误真低级,BS自己一下

[ 本帖最后由 blackanger 于 2007-8-13 20:49 编辑 ]

I.forget('you'){|something| something.remember.deepen}

TOP

我开始还以为是eql就是equal呢,差别大了去了。

TOP

回复 #11 blackanger 的帖子

你好象是写错了。这样的吧:tmp.eql?(arr[4])

要是不对,不要扁我。

TOP

是我写错了。。。写在这警告下自己不要迷糊犯傻

I.forget('you'){|something| something.remember.deepen}

TOP

是男人都会犯错

TOP

2008-12-02 10:12 Crawled by CCBot/1.0 (+http://www.commoncrawl.org/bot.html) @38.103.63.61