为什么Ruby具有TrueClass和FalseClass而不是单个布尔类? [英] Why does Ruby have TrueClass and FalseClass instead of a single Boolean class?

查看:91
本文介绍了为什么Ruby具有TrueClass和FalseClass而不是单个布尔类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我发现这个值时,我正在对值进行序列化. Ruby具有 TrueClass 类和

I was working on serializing values when found out about this one. Ruby has a TrueClass class, and a FalseClass class, but it has no Boolean class. I'd like to know why is this.

我看到了使用Boolean的一些优势;例如,字符串解析可以集中在其上.

I see some advantages in using a Boolean; for example, string parsing could be centralized on it.

Ruby开发人员比我聪明,因此肯定有很多我看不见的充分理由.但现在对我而言,它看起来像是具有OneClassTwoClass而不是 .

Ruby developers are smarter than me, so there must be a lot of good reasons that I just don't see. But right now it looks to me like having OneClass and a TwoClass instead of Fixnum.

推荐答案

似乎Matz自己在他的回答的简短版本:现在可以正常工作,添加布尔值没有任何好处".

Short version of his answer: "right now it works ok, adding a Boolean doesn't give any advantage".

我个人不同意;前述的字符串解析"就是一个例子.另一个问题是,当您根据变量的类型对变量进行不同的处理时,具有Boolean类的变量(即yml解析器)很方便-会删除一个"if".它看起来也更正确,但这是个人观点.

Personally I don't agree with that; the aforementioned "string parsing" is one example. Another one is that when you are applying different treatment to a variable depending on its type, (i.e. a yml parser) having a "Boolean" class is handy - it removes one "if". It also looks more correct, but that's a personal opinion.

这篇关于为什么Ruby具有TrueClass和FalseClass而不是单个布尔类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆