TrueClass/FalseClass vs. Boolean

On Sat, 1 Apr 2006, Niklas Frykholm wrote:

or this

while maybe
print “hello”
end

in a conditional it should evaluate to true/false radomly - compute a
random
number and look at the last bit for instance, 0 -> false, 1 -> true.

harp:~ > cat a.rb
def maybe() [rand].pack(‘f’).unpack(‘c’).first[0].zero? end

puts “hello” while maybe

harp:~ > ruby a.rb
hello
hello

harp:~ > ruby a.rb
hello
hello
hello
hello

harp:~ > ruby a.rb
hello

harp:~ > ruby a.rb
hello

cheers.

-a

On 3/31/06, [email protected] [email protected] wrote:

On Mar 31, 2006, at 5:00 PM, Florian F. wrote:

Or just use the literal constants, that already exist:
case y
when Integer
when true, false
end

Another example where the Ruby ‘does the right thing’.
Thanks for the reminder.

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.
I thought it might be because in math Boolean is not really a scalar
or a quantifiable value. Or in sortable collection terms, it doesn’t
implement Comparable. Other languages work with sorting on booleans
because they might bind false to 0 and true to > 0. So easily fixed by
the to_i above.

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?

Ed

On Jan 19, 2007, at 3:20 PM, Ed Howland wrote:

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 W.

Ed Howland wrote:

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