--001485f450ce843d0b048e7f464e
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: quoted-printable

On Mon, Aug 23, 2010 at 8:52 AM, Ralph Shnelvar <ralphs / dos32.com> wrote:

> irb(main):001:0> class X
> irb(main):002:1> end
> => nil
> irb(main):003:0> x = X.new
> => #<X:0x4d3a660>
> irb(main):004:0> x === X
> => false   # this is surprising
> irb(main):005:0> X === x
> => true    # this is the test I want ... but was surprised when
>           # x === X didn't work.
> irb(main):006:0>
> - - - - -
>
> Does x === X ever mean anything useful?


Typically, you define X#=== to be whatever is meaningful for you. So the
answer depends on how you define it. Typically, when considering how you
want to define it, you should be thinking about how you want it used in a
case statement. For example, classes typically are used in a case statement
to check if an object is an instance of that class:

X = Class.new
x = X.new
case x
when String
  puts "Do stringy thing"
when Regexp
  puts "Do regexy thing"
when X
  puts "Do Xy thing"
else
  puts "Don't know what to do"
end

Is the same as

X = Class.new
x = X.new
if String === x
  puts "Do stringy thing"
elsif Regexp === x
  puts "Do regexy thing"
elsif X === x
  puts "Do Xy thing"
else
  puts "Don't know what to do"
end


In your example, note that the reason X === x in this case works, is because
X inherits its === method from Module where "Case Equalityͳeturns true if
anObject is an instance of mod or one of modÁÔ descendents. Of limited use
for modules, but can be used in case statements to classify objects by
class." http://ruby-doc.org/core/classes/Module.html#M001666


If you want to think of it as an operator, think of it like the minus sign,
where 2-1 is not equal to 1-2, but probably the best thing would be to not
think of it as an operator, but rather as a method exclusively intended for
the convenience of case statements. Notice that the docs even preceed the
description with "Case Equality".

--001485f450ce843d0b048e7f464e--