Ruby 中有原始类型吗? [英] Are there primitive types in Ruby?

查看:38
本文介绍了Ruby 中有原始类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名 Java 开发人员,刚刚开始学习 Ruby.Ruby 有任何原始类型吗?我似乎无法找到它们的列表.如果没有,为什么?

I'm a Java developer who is just starting to learn Ruby. Does Ruby have any primitive types? I can't seem to find a list of them. If not, why?

推荐答案

Ruby 的一个核心原则是所有数据都应该表示为对象.其他语言如 Smalltalk 遵循类似的范例.

A core principle of Ruby is that all data should be represented as objects. Other languages such as Smalltalk follow a similar paradigm.

这种设计的好处是它使 Ruby 更加优雅和易于学习.适用于对象的规则始终适用于所有 Ruby.

The benefit of this design is that it makes Ruby more elegant and easier to learn. The rules applying to objects are consistently applied to all of Ruby.

例如,当初学者第一次学习 Java 时,原始类型 int 和包装类 Integer 之间的区别可能会令人困惑.两者之间通过自动装箱进行的有时令人困惑的隐式转换加剧了这种混淆.

For instance, when beginners are first learning Java, the difference between the primitive type int and the wrapper class Integer can be confusing. This confusion is exacerbated by the sometimes confusing implicit conversions between the two via autoboxing.

那么为什么像 Java 或 C# 这样的语言会为原始类型而烦恼呢?答案是性能.与原语相比,创建对象会产生额外的开销.

So why would languages like Java or C# bother with primitive types? The answer is performance. Creating objects incurs additional overhead when compared with primitives.

这篇关于Ruby 中有原始类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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