Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there primitive types in Ruby?

Tags:

ruby

primitive

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?

like image 895
emily Avatar asked Sep 13 '13 15:09

emily


People also ask

How many types of primitive are there?

Primitive types are the most basic data types available within the Java language. There are 8: boolean , byte , char , short , int , long , float and double . These types serve as the building blocks of data manipulation in Java.

Is array a data type in Ruby?

An array is a data structure that represents a list of values, called elements. Arrays let you store multiple values in a single variable. In Ruby, arrays can contain any data type, including numbers, strings, and other Ruby objects.


1 Answers

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

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.

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.

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.

like image 159
dbyrne Avatar answered Oct 11 '22 05:10

dbyrne