加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

为什么不排序或飞船(飞碟)运算符(<=>)在Ruby中的布尔工作

发布时间:2020-12-16 22:10:35 所属栏目:百科 来源:网络整理
导读:在“ Is it possible to sort a list of objects depending on if the individual object’s response to a method?”中,我发现飞碟在布尔上不起作用. 考虑: Ruby 1.8.7: [true,false].sort # = undefined method `=' for true:TrueClass (NoMethodError)t
在“ Is it possible to sort a list of objects depending on if the individual object’s response to a method?”中,我发现飞碟在布尔上不起作用.

考虑:

Ruby 1.8.7:

[true,false].sort # => undefined method `<=>' for true:TrueClass (NoMethodError)
true <=> false     # => undefined method `<=>' for true:TrueClass (NoMethodError)

Ruby 1.9.3:

[true,false].sort # => comparison of TrueClass with false failed (ArgumentError)
true <=> false     # => nil
true <=> true      # => 0
false <=> true     # => nil

它可能与true和false有关,没有规范的排序顺序,因为哪个是第一个?但是,对我来说听起来很弱.

这是一个排序错误吗?

解决方法

布尔值没有自然排序.

Ruby语言设计师可能觉得为开发人员发明一个排序是一个惊喜,所以他们故意排除了比较运算符.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读