3 ms·
Well, integers are immutable (and conceptually a singleton per value) unless you override stuff in FixNum and add instance variables. Here's an evil example tha
by vidarh 15y ago
Well, integers are immutable (and conceptually a singleton per value) unless you override stuff in FixNum and add instance variables. Here's an evil example that works at least in MRI 1.8.6:
class Fixnum
alias :realplus :+
def + b
@a ||= 0
@a = @a.realplus(b)
self.realplus(@a)
end
end
The integer value itself is immutable, though.
Of course all hell will break loose and adding instance variables in certain classes like Fixnum will be extra slow (as if there aren't enough other reasons not to), since MRI uses type-tagging for Fixnum, Symbol and a few others, and so since they're not "real" objects, if you do anything that requires one, it creates one that's stored in a hash table.