Why can't I instantiate Integer class in Ruby? -
i thought classes in ruby can instantiated. what's preventing integer class being instantiated new method?
integer.new # => nomethoderror: undefined method `new' integer:class
there few of those. besides integer, float, , symbol, can't create new instance of trueclass, falseclass , nilclass too.
these classes (and respective instances) special in ruby , handled in specific way internally.
with small integers example, ruby implicitly handles those. instead of creating new "actual" ruby object each integer number (which hugely wasteful), ruby stores numeric value represented object_id. thus, observe in ruby instance of integer class single value im memory (more or less). able pull off, ruby reserves odd object_ids integer values. thus, number 1 has object_id of 3, number 2 has object_id of 5 , on...
due special handling ruby language itself, can't create new integer instance. given integers immutable (that is, can't changed) defined numeric value in first place.
(note works small integers. larger integers, depending on whether running on 32 bit or 64 bit architecture, ruby still internally create real objects if integer number can't fit scheme described above. handled internally ruby , implementation detail of language itself.)
Comments
Post a Comment