7.1. Ruby’s literal constructors

Ruby has a lot of built-in classes. Most of them can be instantiated using new:

str = String.new
arr = Array.new

Some can’t; for example, you can’t create a new instance of the class Integer. But for the most part, you can create new instances of the built-in classes.

In addition, a lucky, select few built-in classes enjoy the privilege of having literal constructors. That means you can use special notation, instead of a call to new, to create a new object of that class.

The classes with literal constructors are shown in table 7.1. When you use one of these literal constructors, you bring a new object into existence. (Although it’s not obvious from the table, it’s worth noting that there’s no new constructor for ...

Get The Well-Grounded Rubyist, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.