1

Possible Duplicate:
=== vs. == in Ruby

Fixnum == 2.class 
#=> true

Fixnum === 2.class
#=> false

why === doesn't work? and how can I know the method .=== belong to(right now I guess it invoke the Object#==, or Object#===), but how can I make sure of that?

Community
  • 1
  • 1
mko
  • 21,334
  • 49
  • 130
  • 191
  • I would assume that `==` means they have the same value, whilst `===` means they are the same object? – TyrantWave Jun 27 '11 at 13:47
  • 1
    As answered many times before on SO, `===` also take the type of the object into account. – Ash Burlaczenko Jun 27 '11 at 13:48
  • 1
    @TyrantWave and @Ash -- `===` doesn't have specifics for type checking. When it's overridden in sub classes of `Object`. For example, `Range#===` does an `include?` on the object. It doesn't care about types in this situation – Lee Jarvis Jun 27 '11 at 14:13
  • 1
    This is a duplicate of [What does the “`===`” operator do in Ruby?](http://StackOverflow.Com/q/4467538/#4467823), [`===` vs. `==` in Ruby](http://StackOverflow.Com/q/3422223/#3422349) and [Ruby compare objects](http://StackOverflow.Com/q/5230096/). – Jörg W Mittag Jun 27 '11 at 14:38
  • @TyrantWave: Then you would assume wrong. – Jörg W Mittag Jun 27 '11 at 14:41
  • @Ash Burlaczenko: As answered many times before on SO, that's (Pardon my French) bullshit. – Jörg W Mittag Jun 27 '11 at 14:42
  • @Jorg: Apparently so. Well, at least I learned something as well =D – TyrantWave Jun 27 '11 at 14:47
  • 1
    @yozloy Please search SO before asking questions. When you enter your question title you would have seen a ton of these questions already answered. You would have had your answer had you clicked one of them. Assuming my answer answers your question please mark it so. I'm going to add the links provided by @sawa and @Jörg W Mittag into my answer – Lee Jarvis Jun 27 '11 at 14:48
  • @Jorg: Yet another FAQ that's a Frequently Wrongly Answered Question, just like getting instance methods, or what `||=` means. – Andrew Grimm Jun 28 '11 at 00:14

2 Answers2

4

Duplicates:

Case equality. See the documentation for Object#===. This method is usually overridden in subclasses of Object. For example, Module#===:

Case Equality--Returns true if anObject is an instance of mod or one of mod’s descendants. Of limited use for modules, but can be used in case statements to classify objects by class.

>> Module.new === Module
=> false
>> Module === Module.new
=> true

Regexp#=== is another one, in which case it's a synonym of =~:

a = "HELLO"
case a
when /^[a-z]*$/; print "Lower case\n"
when /^[A-Z]*$/; print "Upper case\n"
else;            print "Mixed case\n"
end

An example in IRB:

>> "a" === /a/
=> false
>> /a/ === "a"
=> true

Remember, the first one returns false because you're doing === on String which isn't the same thing. In the second example we're doing === on Regexp

And finally, Range is quite a good one it calls include? on the Range object and passes your value in:

>> (1..100) === 3
=> true
>> (1..100) === 300
=> false

For a list of these, check out RubyDoc.info core documentation and search for === in the methods area in the left side frame

Community
  • 1
  • 1
Lee Jarvis
  • 16,031
  • 4
  • 38
  • 40
0

The == usually only checkes the values of both arguments, when ==== checks the types, too.

Alex
  • 365
  • 3
  • 11