Is my understanding correct that there is no way to change the "value"
of an Integer instance?
IOW if I initialized a variable 'i' to "reference" to a Fixnum with
value 0 (using assignment i = 0) then there is no way to change what i
points to, right?
In message "Re: Integer/Fixnum/Bignum are "immutable"?" on Sat, 18 Dec 2010 02:31:50 +0900, Kedar Mhaswade <kedar.mhaswade@gmail.com> writes:
Sorry. Does that mean my understanding is not correct or no, there is no
way?
I meant value of the Integer to be the numeric value it (the actual
object, not its reference) represents.
If your definition of the "value" of an Integer is its numeric value,
you don't have any way to change. Note that in Ruby even a fixnum can have
its own instance variables.
It may even be that you can change the numeric value (not for Fixnum though, but maybe for Bignum) but that should not be done. For all practical purposes you can consider those types immutable.
Kind regards
robert
···
On 17.12.2010 18:31, Kedar Mhaswade wrote:
Yukihiro Matsumoto wrote in post #969105:
In message "Re: Integer/Fixnum/Bignum are "immutable"?" >> on Sat, 18 Dec 2010 02:09:17 +0900, Kedar Mhaswade >> <kedar.mhaswade@gmail.com> writes:
>
>Is my understanding correct that there is no way to change the "value"
>of an Integer instance?
But the exact answer depends on your definition of the "value".
I meant value of the Integer to be the numeric value it (the actual
object, not its reference) represents.
If your definition of the "value" of an Integer is its numeric value,
you don't have any way to change. Note that in Ruby even a fixnum can
have
its own instance variables.
Got it. That clarifies. Thanks!
A related question is if the above were true, shouldn't 2.frozen? have
returned true (or else I don't understand what 2.frozen? means)?
If your definition of the "value" of an Integer is its numeric value,
you don't have any way to change. Note that in Ruby even a fixnum can
have
its own instance variables.
Got it. That clarifies. Thanks!
A related question is if the above were true, shouldn't 2.frozen? have
returned true (or else I don't understand what 2.frozen? means)?
For Fixnum, I believe there are simply no setter methods for the
"numeric value" so the "frozen" state is not required to make the
"numeric value" immutable.
But 'freeze' and 'frozen?' do work on an FixNum object.
RuntimeError: can't modify frozen object
from (irb):8:in `instance_variable_set'
from (irb):8
from /home/peterv/.rvm/rubies/ruby-1.9.2-head/bin/irb:16:in `<main>'
speed = 160 #=> 160 # does not change internal state,
speed.object_id #=> 321 # but now refers a different object