Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > === confusion

Reply
Thread Tools

=== confusion

 
 
Ian Macdonald
Guest
Posts: n/a
 
      02-09-2004
Hello,

After a couple of years of Ruby programming, I find that the ===
operator still has the ability to occasionally confound me.

Today, I had some code that looked like this:

foo = case bar.class
when Foo::Bar
'abc'
when Foo::Bar::Baz
'def'
when Foo::Qux
'ghi'
else
'jkl'
end

The problem is that most comparisons of bar.class fell through to the
else clause. A quick debugging session on a particular instance of 'bar'
revealed that 'bar.class == Foo::Bar' was true, but
'bar.class === Foo::Bar' was false. Quite honestly, I don't understand
why, as I'm not overriding === anywhere. My classes are not derived from
any standard Ruby classes, either, so they should be inheriting ===
from Object, if I understand things correctly.

I fixed it by changing the start of the case statement to
'case bar.class.to_s' and then comparing to strings instead of object
identifiers, but I would like to understand what exactly === was doing
in the original case.

Ian
--
Ian Macdonald | When neither their poverty nor their honor
System Administrator | is touched, the majority of men live
http://www.velocityreviews.com/forums/(E-Mail Removed) | content. -- Niccolo Machiavelli
http://www.caliban.org |
|


 
Reply With Quote
 
 
 
 
George Ogata
Guest
Posts: n/a
 
      02-09-2004
Ian Macdonald <(E-Mail Removed)> writes:

> foo = case bar.class
> when Foo::Bar
> 'abc'
> when Foo::Bar::Baz
> 'def'
> when Foo::Qux
> 'ghi'
> else
> 'jkl'
> end
>
> The problem is that most comparisons of bar.class fell through to the
> else clause. A quick debugging session on a particular instance of 'bar'
> revealed that 'bar.class == Foo::Bar' was true, but
> 'bar.class === Foo::Bar' was false. Quite honestly, I don't understand
> why, as I'm not overriding === anywhere. My classes are not derived from
> any standard Ruby classes, either, so they should be inheriting ===
> from Object, if I understand things correctly.
>
> I fixed it by changing the start of the case statement to
> 'case bar.class.to_s' and then comparing to strings instead of object
> identifiers, but I would like to understand what exactly === was doing
> in the original case.


This keeps coming up from time to time.

Module#=== returns true iff the argument is an instance of the module
or one of its descendants (from the Pickaxe). In your code,
`bar.class' returns an instance of `Class', which is of course not a
descendant of `Foo::Bar', `Foo::Bar::Baz', etc., and so falls through
to the else clause. I think what you want is to remove the `.class'
in the case expression:

foo = case bar
## ^^^^^^ (removed the `.class')
when Foo::Bar
'abc'
when Foo::Bar::Baz
'def'
when Foo::Qux
'ghi'
else
'jkl'
end

HTH
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mozilla & Firefox Confusion listsubscriber@hotmail.com Firefox 9 01-20-2005 05:09 PM
Confusion about location of Mozilla files (Mandrake Linux 10.0) Hallvard Tangeraas Firefox 0 09-14-2004 09:46 AM
Procedures in testbench confusion Peter Hermansson VHDL 2 08-25-2004 02:15 PM
confusion when resetting registers martin f. krafft VHDL 2 08-19-2004 06:29 AM
defined() confusion Chris Perl 2 12-18-2003 06:06 PM



Advertisments