On Sat, Aug 21, 2010 at 12:13 AM, Ralph Shnelvar <ralphs / dos32.com> wrote:

>
> Having read your explanation and Dave Thomas' *Programming Ruby 1.9* book
> about the use of =3D=3D=3D in comparisons in case statements ...
>
> Is it he fact that *case* uses =3D=3D (two =3D's) for non-Class *when's* =
and uses
> =3D=3D=3D (three =3D's) for *when's* that are Classes?
>
No (I think the following is right, but I'd welcome any corrections) - case
always uses "=3D=3D=3D", it's just that the meaning of "=3D=3D=3D" may be d=
ifferent to
"=3D=3D".

1. case always uses "=3D=3D=3D" for comparisons; in
  case x
  when y then
the test is y =3D=3D=3D x

http://ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html

Common comparison operators, Operator Meaning
=3D=3D Test for equal value.
=3D=3D=3D Used to test equality within a when clause of a case statement.

case operates by comparing the target (the expression after the keyword
case) with each of the comparison expressions after the when keywords. This
test is done using comparison =3D=3D=3D target. As long as a class defines
meaningful semantics for =3D=3D=3D (and all the built-in classes do), objec=
ts of
that class can be used in case expressions.
...
Ruby classes are instances of class Class, which defines =3D=3D=3D as a tes=
t to
see if the argument is an instance of the class or one of its superclasses.

2. http://www.ruby-doc.org/core/classes/Object.html#M000345
obj =3D=3D=3D other =3D> true or false
Case Equality=97For class Object, effectively the same as calling #=3D=3D, =
but
typically overridden by descendents to provide meaningful semantics in case
statements.

3. http://www.ruby-doc.org/core/classes/Module.html#M001666
(remembering that Class is a subclass of Module, which I forget when I
initially couldn't find the "=3D=3D=3D" method in Class)
mod =3D=3D=3D obj =3D> true or false
Case Equality=97Returns true if anObject is an instance of mod or one of mo=
d=91s
descendents. Of limited use for modules, but can be used in case statements
to classify objects by class.

Putting that all together, and using the String class as an example:
x =3D "Karel Capek"
y =3D "Karel Capek"
p x.object_id #=3D> 1880688
p y.object_id #=3D> 1880656, so x & y are different objects
p x =3D=3D y #=3D> true
p x =3D=3D=3D y #=3D> true, so for String objects "=3D=3D=3D" is the same a=
s "=3D=3D"
            # at least for comparing String objects with String objects

p x =3D=3D String #=3D> false
p x =3D=3D=3D String #=3D> false

p x.class =3D=3D String #=3D> true
p String =3D=3D String #=3D> true
p x.class =3D=3D=3D String #=3D> false
p String =3D=3D=3D String #=3D> false

p String =3D=3D x #=3D> false
p String =3D=3D=3D x #=3D> true; this is what is being used in the case sta=
tement
                 # case x; when String then true; else false; end
p String =3D=3D x.class #=3D> true
p String =3D=3D=3D x.class #=3D> false; same as String =3D=3D=3D String