"T. Onoma" <transami / runbox.com> schrieb im Newsbeitrag
news:200312132208.22847.transami / runbox.com...
> On Sunday 14 December 2003 05:51 am, Hal Fulton wrote:
> > T. Onoma wrote:
> > > is there anyway, anyway at all, ugly hacks accepted, of doing
inplace
> > > assignment in Ruby?
> >
> > Not sure what you mean, can you give an example from
> > some other language? Or just explain?
> >
> > Hal
>
> Sure,
>
>   q = 1
>   p q.__id__  # => 3
>   q = 2
>   p q.__id__  # => 5  (want this to still be 3)
>
> In other words I want to change what q "contains" rather then alter its
> reference. With an array for example you can do that with #replace.

You want to change the state of the instance at hand and don't want to
change its identity.  It depends on the type of instance at hand whether
you can achieve your goal: it doesn't work for integers, but you can
change a String (which also works for constants):

irb(main):016:0> s = "foo"
=> "foo"
irb(main):017:0> s.id
=> 135086644
irb(main):018:0> s = "bar"
=> "bar"
irb(main):019:0> s.id
=> 135071956
irb(main):020:0> s.replace "foo"
=> "foo"
irb(main):021:0> s.id
=> 135071956
irb(main):022:0>

There is no general solution to what you want.  Personally I never needed
this feature.  Normally you can solve this with proper nesting.

> In particular I'm interesed in doing this with constants.

Keep in mind that the constness of constants is all constants are about.
So changing them is generally not a good idea if it's a simple type like
String or Integer.

Regards

    robert