[ruby-core:39304] [Ruby 1.9 - Bug #5285][Open] Ruby 1.9.2 throws exception on sort of array containing true AND false values

1 view
Skip to first unread message

Martin Corino

unread,
Sep 6, 2011, 10:17:29 AM9/6/11
to ruby...@ruby-lang.org

Issue #5285 has been reported by Martin Corino.

----------------------------------------
Bug #5285: Ruby 1.9.2 throws exception on sort of array containing true AND false values
http://redmine.ruby-lang.org/issues/5285

Author: Martin Corino
Status: Open
Priority: Normal
Assignee:
Category: core
Target version: 1.9.2
ruby -v: ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]


I encountered a very weird bug.

Sorting of arrays containing identical boolean values (ALL true or ALL false) works fine.
As soon as 1 value differs however #sort throws an exception.


ruby-1.9.2-p290 :001 > [true, true].sort
=> [true, true]

ruby-1.9.2-p290 :002 > [false, false].sort
=> [false, false]

ruby-1.9.2-p290 :003 > [false, true].sort
ArgumentError: comparison of FalseClass with true failed
from (irb):3:in `sort'
from (irb):3
from /home/martin/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'

ruby-1.9.2-p290 :004 > [false, true, false].sort
ArgumentError: comparison of FalseClass with true failed
from (irb):4:in `sort'
from (irb):4
from /home/martin/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'

ruby-1.9.2-p290 :011 > [true, true, true, true].sort
=> [true, true, true, true]

ruby-1.9.2-p290 :012 > [true, true, true, false].sort
ArgumentError: comparison of TrueClass with false failed
from (irb):12:in `sort'
from (irb):12
from /home/martin/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'

--
http://redmine.ruby-lang.org

Yui NARUSE

unread,
Sep 6, 2011, 10:53:32 AM9/6/11
to ruby...@ruby-lang.org

Issue #5285 has been updated by Yui NARUSE.

Status changed from Open to Rejected

Sort uses <=>.
true <=> true and false <=> false work, but true <=> false.
Simply because of this.


----------------------------------------
Bug #5285: Ruby 1.9.2 throws exception on sort of array containing true AND false values
http://redmine.ruby-lang.org/issues/5285

Author: Martin Corino
Status: Rejected

Pedro Medeiros

unread,
Sep 6, 2011, 1:30:50 PM9/6/11
to ruby...@ruby-lang.org

Issue #5285 has been updated by Pedro Medeiros.


actually, by doing true <=> false on irb returns nil, while doing true <=> true or false <=> false will return 0. not necessarily an exception. Wouldn't it be right to return -1 or 1 by comparing true with false or false with true, even though true came from TrueClass and false came from FalseClass ?


----------------------------------------
Bug #5285: Ruby 1.9.2 throws exception on sort of array containing true AND false values
http://redmine.ruby-lang.org/issues/5285

Author: Martin Corino
Status: Rejected

Michael Edgar

unread,
Sep 6, 2011, 2:08:11 PM9/6/11
to ruby...@ruby-lang.org
Booleans are not ordered. It doesn't make sense to say "the logical value true is less than/greater than the logical value false" - that statement doesn't mean anything. I can't help but feel sorting an enumerable of booleans is a nonsensical operation. I'm not even sure what purpose it would serve that wouldn't more reasonably be modeled by another (likely more efficient) operation.
Reply all
Reply to author
Forward
0 new messages