TrueClass/FalseClass vs. Boolean

G

gwtmp01

But in terms of Ruby "doing the right thing", you'd expect
[false, true, false].sort
--> [false, false, true]
because it does this just about everywhere else. This is such a common
idiom in CS, that I think Ruby should implement it. But then, I only
have just the 2 cents. Anyone know the main reason it does not?

interesting observation

Since true and false are not instances of the same class it is
like asking how to compare 4 and :foobar or 6.32 and "tuesday".

One possibility would be to have <=> be defined in terms of object_id
by default similar to how == is defined. Since false.object_id is 0
and true.object_id is 2 that does give you [false,false,true]
in your example but for an obscure, implementation dependent reason.

Gary Wright
 
J

Justin Collins

Ed said:
Another, related issue, is the lack of the <=> operator for either
class. This comes in handy when sorting arrays of booleans or using
sort_by on a collection returned from activerecord that has a boolean
attribute.

I know this has been covered in the archives of this list before, but
I wasn't able to find any definitive answer from Matz or someone else.

The reason, IIRC, was the lack of meaning for true > false, false >
true, etc.

-Justin
 

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,222
Messages
2,571,141
Members
47,756
Latest member
JulienneY0

Latest Threads

Top