Ruby’s ::new Class
::new
is a class method: it creates an instance of the class.
Ruby generally offers two ways to create an object. For example the Array object can be created the following ways:
array1 = Array.new
(the::new
class method)array2 = []
(a shorthand method)
Functionnally and speed is the same, however Array.new
can be called passing in a block, while []
cannot.
Codes behing written for people first, linter such as Rubocop prefers1 []
for creating empty Array and Array.new(n)
when creating an Array object, passing in parameter n
.