From: Jim Weirich Date: 2003-08-05T09:26:39+09:00 Subject: Re: clearing a parameter in Ruby? On Mon, 2003-08-04 at 13:36, Roy Patrick Tan wrote: > Hi, > > is it possible to write a method that can "clear" a parameter in Ruby? > That is can a 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"? I don't recommend this (because I think I would get really annoyed if I were using a library that did this). But with the above disclaimer, you could do it this way: class Pusher def initialize @stack = Array.new end def push(&block) sym = block.call @stack.push(eval(sym.to_s, block)) eval "#{sym.to_s} = nil", block end end s = Pusher.new x = "HI" s.push { :x } p x # => nil -- -- Jim Weirich jweirich@one.net http://onestepback.org ----------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)