From: Robert Klemme Date: 2003-08-05T18:18:45+09:00 Subject: Re: clearing a parameter in Ruby? "Roy Patrick Tan" schrieb im Newsbeitrag news:bgmcv4$orh$1@solaris.cc.vt.edu... > 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. class SomeContainer # add a frozen copy def add_1(obj) add_internal obj.clone.freeze end # deep copy the obj (i.e. the complete object graph) and store it # nobody outside knows any of these instances def add_2(obj) add_internal Marshal.load(Marshal.dump(obj)) end private def add_internal(obj) # put into the collection end end The problem here is that you typically deal with graphs of instances. If you freeze only the one put into the collection, there might still be changes possible that defy your aim. If you copy a complete graph you are sure that nobody changes state but you might loose references to instances that you wanted to keep. It's a tradeoff. > 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. Aha! Akademic research. Regards robert