Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I instantiate Integer class in Ruby?

Tags:

ruby

I thought all classes in Ruby can be instantiated. What's preventing Integer class from being instantiated with new method?

Integer.new
# => NoMethodError: undefined method `new' for Integer:Class
like image 738
Maximus S Avatar asked Aug 15 '17 12:08

Maximus S


People also ask

How do you instantiate a class in Ruby?

An object instance is created from a class through the process called instantiation. In Ruby this takes place through the Class method new . This function sets up the object in memory and then delegates control to the initialize function of the class if it is present.

Is Integer a method in Ruby?

5. integer? : This method always returns true for integer value and false for non-integer. The return type of this method is boolean.

What is the size of an integer data type in Ruby?

What is the size of an integer data type in ruby? Explanation: Integer ranges from -230 to 229 and lies in object of class Fixnum. 6. Ruby can deal with both numbers and floating point values.

What is TO_S in Ruby?

The to_s function in Ruby returns a string containing the place-value representation of int with radix base (between 2 and 36). If no base is provided in the parameter then it assumes the base to be 10 and returns.


2 Answers

There are a few of those. Besides Integer, Float, and Symbol, you can't create a new instance of TrueClass, FalseClass and NilClass too.

These classes (and their respective instances) are all special in Ruby and are handled in a specific way internally.

With small Integers for example, Ruby implicitly handles those. Instead of creating a new "actual" Ruby object for each integer number (which would be hugely wasteful), Ruby stores those as only their numeric value represented by the object_id. Thus, what you observe in Ruby as an instance of the Integer class is actually a single value im memory (more or less). To be able to pull this off, Ruby reserves all odd object_ids for integer values. Thus, the number 1 has the object_id of 3, the number 2 has the object_id of 5 and so on...

Due to this special handling by the Ruby language itself, you can't create a new Integer instance. Now given that Integers themselves are always immutable (that is, they can't be changed) they are only defined by their numeric value in the first place.

(Note that this only works for small integers. For larger integers, depending on whether you are running on a 32 bit or 64 bit architecture, Ruby will still internally create real objects if the integer number can't fit into the scheme described above. This is however handled internally by Ruby and is basically an implementation detail of the language itself.)

like image 121
Holger Just Avatar answered Oct 13 '22 02:10

Holger Just


You can't allocate heap objects of an Integer in Ruby. In Ruby Integers are immediates which means you cannot have an instantiated version of the object. Since you can’t allocate them, you can’t create a subclass and allocate instances of the subclass.

like image 37
Billy Ferguson Avatar answered Oct 13 '22 01:10

Billy Ferguson