Integer/Fixnum/Bignum are "immutable"?

K

Kedar Mhaswade

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?

Regards,
Kedar
 
K

Kedar Mhaswade

Yukihiro Matsumoto wrote in post #969105:
Hi,

In message "Re: Integer/Fixnum/Bignum are "immutable"?"
on Sat, 18 Dec 2010 02:09:17 +0900, Kedar Mhaswade
|
|Is my understanding correct that there is no way to change the "value"
|of an Integer instance?

No.

Sorry. Does that mean my understanding is not correct or no, there is no
way?
But the exact answer depends on your definition of the "value".

matz.

I meant value of the Integer to be the numeric value it (the actual
object, not its reference) represents.
 
K

Kedar Mhaswade

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)?
 
R

Robert Klemme

Yukihiro Matsumoto wrote in post #969105:

I meant value of the Integer to be the numeric value it (the actual
object, not its reference) represents.

You can add instance variables to some of the types you mention so they
are not completely immutable:

irb(main):001:0> b=1<<100
=> 1267650600228229401496703205376
irb(main):002:0> b.class
=> Bignum
irb(main):003:0> b.instance_variable_set '@x', "foo"
=> "foo"
irb(main):004:0> b.instance_variable_get '@x'
=> "foo"
irb(main):005:0>

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
 
P

Peter V.

Kedar Mhaswade wrote in post #969114:
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.

$ irb #ruby-1.9.2-head
speed = 150 #=> 150
speed.object_id #=> 301
speed.frozen? #=> false
speed.instance_variable_set '@accuracy', '+/- 2' #=> '+/- 2'
speed.freeze #=> 150
speed.frozen? #=> true
speed.instance_variable_set '@accuracy', '+/- 1'
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 ` said:
speed = 160 #=> 160 # does not change internal state,
speed.object_id #=> 321 # but now refers a different object

HTH,

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,365
Latest member
BurtonMeec

Latest Threads

Top