Forum: Ruby === operator

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5b4cc2b3895cc34e3bcef32025c3cafd?d=identicon&s=25 Jon Leighton (turnip)
on 2007-06-06 22:17
Hi,

>> {} === Hash
=> false
>> case {}
>> when Hash then true
>> end
=> true
>> Hash === {}
=> true

Why is the first statement false when the second is true? I understand
case uses === to compare the objects? Perhaps it switches them round as
in the last statement?

Cheers,

Jon
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-06-06 22:26
(Received via mailing list)
Hi --

On Thu, 7 Jun 2007, Jon Leighton wrote:

>
> Why is the first statement false when the second is true? I understand
> case uses === to compare the objects? Perhaps it switches them round as
> in the last statement?

Yes; === is called on the when expression(s), with the case object as
argument.


David
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-06-06 22:31
(Received via mailing list)
On 6/6/07, Jon Leighton <turnip@turnipspatch.com> wrote:
> Hi,

Maybe a more explicit notation helps
>
> >> {} === Hash
{}.send(:===, Hash)
> => false
> >> case {}
> >> when Hash then true
when Hash.send(:===, {}) then true
> >> end
> => true
> >> Hash === {}
> => true
>
> Why is the first statement false when the second is true? I understand
> case uses === to compare the objects? Perhaps it switches them round as
> in the last statement?
No the LHS is the receiver and the RHS is the first argument of the
message.
{}.send(:===, Hash) the === instance_method of Hash is called which is
inherited from Object and basically means equality, hence false.

Hash.send(:===, {}) the class method of Hash is called which is
inherited from Class and means is_a? hence true.

HTH
Robert
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-06-06 22:33
(Received via mailing list)
On 6/6/07, dblack@wobblini.net <dblack@wobblini.net> wrote:
> Hi --
<snip>
> Yes; === is called on the when expression(s), with the case object as
> argument.
Well you are right of course, I said now because I did not understand
what OP meant :(

Robert
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2007-06-07 05:01
(Received via mailing list)
Am Donnerstag, 07. Jun 2007, 05:32:38 +0900 schrieb Robert Dober:
> On 6/6/07, dblack@wobblini.net <dblack@wobblini.net> wrote:
> <snip>
> >Yes; === is called on the when expression(s), with the case object as
> >argument.
> Well you are right of course, I said now because I did not understand
> what OP meant :(

Some may say it's an unexpected behaviour. Others will find
themselves detecting it as a welcome surprise. This is Ruby
at its best.

Bertram
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-06-07 13:03
(Received via mailing list)
Hi --

On Thu, 7 Jun 2007, Bertram Scharpf wrote:

> at its best.
I always thought it was just the logical way to do the case statement.
Since you're testing something about the case object, you may not know
what it is or what its === method does:

   case x     # what is x?
   when 1     ...
   when "yes" ...
   when C
   when nil   ...
   end

So I don't think it would make sense to have a case construct where
=== was called on x.


David
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-06-07 18:01
(Received via mailing list)
On 07.06.2007 13:02, dblack@wobblini.net wrote:
>>> what OP meant :(
>   when 1     ...
>   when "yes" ...
>   when C
>   when nil   ...
>   end
>
> So I don't think it would make sense to have a case construct where
> === was called on x.

Another reason why that would be an odd way to do it: *all* tests then
would have to be implemented in x's class - now how much sense would
that make to do that?  Just think of the type test (i.e. using class
objects in when clause)...

Kind regards

  robert
This topic is locked and can not be replied to.