Ruby中有原始类型吗?

时间:2023-01-27 08:52:56

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的Java开发人员。 Ruby有没有原始类型?我似乎无法找到它们的清单。如果没有,为什么?

3 个解决方案

#1


27  

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

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

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.

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

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时,基本类型int和包装类Integer之间的区别可能会令人困惑。通过自动装箱这两者之间有时令人困惑的隐含转换加剧了这种混乱。

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.

那么为什么像Java或C#这样的语言会受到原始类型的困扰?答案是表现。与基元相比,创建对象会产生额外的开销。

#2


12  

There are no primitive data types in Ruby. Every value is an object, even literals are turned into objects:

Ruby中没有原始数据类型。每个值都是一个对象,甚至文字都变成了对象:

nil.class      #=> NilClass
true.class     #=> TrueClass
'foo'.class    #=> String
100.class      #=> Integer
0x1a.class     #=> Integer
0b11010.class  #=> Integer
123.4.class    #=> Float
1.234e2.class  #=> Float

This allows you to write beautiful code:

这允许您编写漂亮的代码:

3.times do
  puts "Hello from Ruby"
end

#3


5  

Quoting from About Ruby

引自关于Ruby

In Ruby, everything is an object. Every bit of information and code can be given their own properties and actions.

在Ruby中,一切都是对象。每一点信息和代码都可以赋予它们自己的属性和动作。

In many languages, numbers and other primitive types are not objects. Ruby follows the influence of the Smalltalk language by giving methods and instance variables to all of its types. This eases one’s use of Ruby, since rules applying to objects apply to all of Ruby.

在许多语言中,数字和其他基本类型不是对象。 Ruby通过为所有类型提供方法和实例变量来遵循Smalltalk语言的影响。这简化了对Ruby的使用,因为应用于对象的规则适用于所有Ruby。

Java chooses to preserve some primitive types mainly for performance, but you have to admit, not every type is a class does makes Java code a little awkward sometimes. The philosophy of Ruby is to make the programmer's days easier, I think making everything an object is one way to achieve this.

Java选择保留一些原始类型主要是为了性能,但你不得不承认,并不是每个类都是一个类,有时会使Java代码有点尴尬。 Ruby的哲学是让程序员的日子变得更容易,我认为将一切都变成对象是实现这一目标的一种方式。

#1


27  

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

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

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.

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

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时,基本类型int和包装类Integer之间的区别可能会令人困惑。通过自动装箱这两者之间有时令人困惑的隐含转换加剧了这种混乱。

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.

那么为什么像Java或C#这样的语言会受到原始类型的困扰?答案是表现。与基元相比,创建对象会产生额外的开销。

#2


12  

There are no primitive data types in Ruby. Every value is an object, even literals are turned into objects:

Ruby中没有原始数据类型。每个值都是一个对象,甚至文字都变成了对象:

nil.class      #=> NilClass
true.class     #=> TrueClass
'foo'.class    #=> String
100.class      #=> Integer
0x1a.class     #=> Integer
0b11010.class  #=> Integer
123.4.class    #=> Float
1.234e2.class  #=> Float

This allows you to write beautiful code:

这允许您编写漂亮的代码:

3.times do
  puts "Hello from Ruby"
end

#3


5  

Quoting from About Ruby

引自关于Ruby

In Ruby, everything is an object. Every bit of information and code can be given their own properties and actions.

在Ruby中,一切都是对象。每一点信息和代码都可以赋予它们自己的属性和动作。

In many languages, numbers and other primitive types are not objects. Ruby follows the influence of the Smalltalk language by giving methods and instance variables to all of its types. This eases one’s use of Ruby, since rules applying to objects apply to all of Ruby.

在许多语言中,数字和其他基本类型不是对象。 Ruby通过为所有类型提供方法和实例变量来遵循Smalltalk语言的影响。这简化了对Ruby的使用,因为应用于对象的规则适用于所有Ruby。

Java chooses to preserve some primitive types mainly for performance, but you have to admit, not every type is a class does makes Java code a little awkward sometimes. The philosophy of Ruby is to make the programmer's days easier, I think making everything an object is one way to achieve this.

Java选择保留一些原始类型主要是为了性能,但你不得不承认,并不是每个类都是一个类,有时会使Java代码有点尴尬。 Ruby的哲学是让程序员的日子变得更容易,我认为将一切都变成对象是实现这一目标的一种方式。