sheesh - can't you tell it looks longer!

-a
···
On Sun, 2 Apr 2006, Joel VanderWerf wrote:
ara.t.howard@noaa.gov wrote:
def maybe() [rand].pack('f').unpack('c').first[0].zero? end
What does
[rand].pack('f').unpack('c').first[0]
do that
rand(2)
doesn't?
--
share your knowledge. it's a way to achieve immortality.
- h.h. the 14th dali lama
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 3/31/06, gwtmp01@mac.com <gwtmp01@mac.com> wrote:
On Mar 31, 2006, at 5:00 PM, Florian Frank 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.
--
Ed Howland
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
···
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?
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