Integer/Fixnum/Bignum are "immutable"?

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

···

--
Posted via http://www.ruby-forum.com/.

Hi,

···

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?

No. But the exact answer depends on your definition of the "value".

              matz.

Yukihiro Matsumoto wrote in post #969105:

Hi,

>
>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.

···

In message "Re: Integer/Fixnum/Bignum are "immutable"?" > on Sat, 18 Dec 2010 02:09:17 +0900, Kedar Mhaswade > <kedar.mhaswade@gmail.com> writes:

--
Posted via http://www.ruby-forum.com/\.

Hi,

···

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.

              matz.

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

···

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.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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

···

--
Posted via http://www.ruby-forum.com/\.

Kedar Mhaswade wrote in post #969114:

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.

$ 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 `<main>'

speed = 160 #=> 160 # does not change internal state,
speed.object_id #=> 321 # but now refers a different object

HTH,

Peter

···

--
Posted via http://www.ruby-forum.com/\.