inplace assignment

T

T. Onoma

is there anyway, anyway at all, ugly hacks accepted, of doing inplace
assignment in Ruby?
 
H

Hal Fulton

T. Onoma said:
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
 
T

T. Onoma

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. In
particular I'm interesed in doing this with constants.
 
N

nobu.nokada

Hi,

At Sun, 14 Dec 2003 15:14:44 +0900,
T. Onoma said:
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. In
particular I'm interesed in doing this with constants.

Array#replace isn't concerned with constants.

A = [1]
A.replace([2])
p A[0] # => 2
 
H

Hal Fulton

T. Onoma said:
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. In
particular I'm interesed in doing this with constants.

OK, I thought that was what you meant.

String also has a replace. But there's no general Object#replace.

My impression is that this in impossible in general, and for
immediate values such as Fixnums, "even more impossible."

The idea of doing this with a constant is scary to me. It's bad
enough that a constant String or Array can be changed. And it's
even scarier to think of doing that with something like a
Fixnum.

What's the situation where you'd want to do this?

Hal
 
G

Gavin Sinclair

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. In
particular I'm interesed in doing this with constants.

For "constants" I presume you mean "integers" here? (Constants can be
of any class, and simply mean a variable that begins with a capital
letter, and which are thinly guarded against reassignment.)

You certainly can't do what you're asking in Ruby. Fixnum values are
hardcoded objects for performance reasons.

Fixnums aside, there's nothing you can do to change the behaviour of
"=".

Gavin
 
H

Hal Fulton

Hi,

At Sun, 14 Dec 2003 15:14:44 +0900,
T. Onoma said:
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. In
particular I'm interesed in doing this with constants.


Array#replace isn't concerned with constants.

A = [1]
A.replace([2])
p A[0] # => 2

Thanks for pointing this out. I don't think I ever noticed that.

Should this give an error?

Hal
 
N

nobu.nokada

Hi,

At Sun, 14 Dec 2003 15:50:54 +0900,
Hal said:
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. In
particular I'm interesed in doing this with constants.


Array#replace isn't concerned with constants.

A = [1]
A.replace([2])
p A[0] # => 2

Thanks for pointing this out. I don't think I ever noticed that.

Should this give an error?

No. A constant in Ruby is a name which can point to only
particular object. Nothing related to the container's
contents.
 
T

T. Onoma

OK, I thought that was what you meant.

String also has a replace. But there's no general Object#replace.

My impression is that this in impossible in general, and for
immediate values such as Fixnums, "even more impossible."

The idea of doing this with a constant is scary to me. It's bad
enough that a constant String or Array can be changed. And it's
even scarier to think of doing that with something like a
Fixnum.

What's the situation where you'd want to do this?

Well, the reason its a constant is b/c its a class. It's funny how things come
up. I only recently learned that when you modify a class, all previously
defined objects of that class are effected. But what if you want to pull the
entire "rug out", so to speak, and replace a class with a "duck similiar"
class?

Okay, so it's a bit crazy. But I actually came across a use for this. Luckly
my situation is specialized --I am replacing the class with a subclass of it,
so I discovered that I could just do this:

class MyClass < MyClass

(and now that I think about it I can probably do this for any calss) it seems
to work fine.

but now i'm running into an disturbing problem. next post...
 
M

Mauricio Fernández

OK, I thought that was what you meant.

String also has a replace. But there's no general Object#replace.

My impression is that this in impossible in general, and for
immediate values such as Fixnums, "even more impossible."

The idea of doing this with a constant is scary to me. It's bad

Don't you like open classes? ;-)
enough that a constant String or Array can be changed. And it's
even scarier to think of doing that with something like a
Fixnum.

--
_ _
| |__ __ _| |_ ___ _ __ ___ __ _ _ __
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

/*
* Please skip to the bottom of this file if you ate lunch recently
* -- Alan
*/
-- from Linux kernel pre-2.1.91-1
 
H

Hal Fulton

Hi,

At Sun, 14 Dec 2003 15:50:54 +0900,
Hal said:
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. In
particular I'm interesed in doing this with constants.


Array#replace isn't concerned with constants.

A = [1]
A.replace([2])
p A[0] # => 2

Thanks for pointing this out. I don't think I ever noticed that.

Should this give an error?


No. A constant in Ruby is a name which can point to only
particular object. Nothing related to the container's
contents.

Actually, I completely misread your example. I already understood this
phenomenon.

I thought that the assignment was silently discarded, i.e., I read this
as:

A = [1]
A.replace([2])
p A[0] # => 1

Sorry for being stupid. :)

Hal
 
G

gabriele renzi

il Sun, 14 Dec 2003 15:50:54 +0900, Hal Fulton
Thanks for pointing this out. I don't think I ever noticed that.

Should this give an error?

every 'changeable' object, such has String, Hash, Array etc works this
way. The point is that the object refernce is constant, not the
object.
IMO this is a work for freeze() , if you really want an error
 
H

Hal Fulton

gabriele said:
every 'changeable' object, such has String, Hash, Array etc works this
way. The point is that the object refernce is constant, not the
object.
IMO this is a work for freeze() , if you really want an error

Thank you. Yes, I knew this already. My brain was not working properly.

Thanks,
Hal
 
R

Robert Klemme

T. Onoma said:
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
 

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
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top