From: Roy Patrick Tan Date: 2003-09-17T23:24:25+09:00 Subject: Re: clearing a parameter in Ruby? Austin Ziegler wrote: > On Tue, 5 Aug 2003 02:36:32 +0900, Roy Patrick Tan wrote: > >>is it possible to write a method that can "clear" a parameter in Ruby? >>That is can I write a method "push" such that for the code below: >> >>x = "Hello" >>string_stack.push(x) >>puts x >> >>the output will be nil instead of "Hello"? .... > Why do you wish to do this? We might be able to help you figure out a better > way to do what you're trying to do. In essence, what I'm trying to do is create a set of container classes that, when an object is inserted into a container, the container guarantees that the object does not change state until it is taken out again. The problem is that in Ruby (and almost all modern imperative languages) two variables can refer to the same object. So part of the solution is never to use assignment except when creating an object (if you need a copy of an object, clone it). But that still leaves us w parameter passing, since a variable in the caller environment refers to an object that is also referred to by a variable in the called method... so I wanted to be able to clear the caller's variable. I thought of using a SimpleDelegator in a fashion similar to Mr. Young's solution: x = SomeComplexObject.new x = SimpleDelegator.new(x) push(x) But I was hoping there was a simple way to achieve this (for example, using something similar to TCL's upvar). I guess your real question is, what kind of software am I building that requires these kind of components? I don't really need them, I was writing what was supposed to be a quick and dirty program when I realized how totally reliant I am on aliasing, and wondered how easy it would be to implement in Ruby some of the non-aliasing components I've been studying. Anyway thanks to everyone who took the time to answer my question