=== operator

J

Jon Leighton

Hi,
=> 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
 
D

dblack

Hi --

Hi,

=> 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?

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


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
R

Robert Dober


Maybe a more explicit notation helps
{}.send:)===, Hash)
=> false
=> 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
 
B

Bertram Scharpf

Am Donnerstag, 07. Jun 2007, 05:32:38 +0900 schrieb Robert Dober:
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
 
D

dblack

Hi --

Am Donnerstag, 07. Jun 2007, 05:32:38 +0900 schrieb Robert Dober:

Some may say it's an unexpected behaviour. Others will find
themselves detecting it as a welcome surprise. This is Ruby
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

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
R

Robert Klemme

Hi --



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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,261
Messages
2,571,308
Members
47,970
Latest member
DelGamboa0

Latest Threads

Top