T
T. Onoma
is there anyway, anyway at all, ugly hacks accepted, of doing inplace
assignment in Ruby?
assignment in Ruby?
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. 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.
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.
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.
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
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?
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?
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?
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.
Mauricio said:Don't you like open classes? ;-)
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.
Thanks for pointing this out. I don't think I ever noticed that.
Should this give an error?
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
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.
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.