From: "Jesús Gabriel y Galán" Date: 2010-10-21T23:52:59+09:00 Subject: Re: about handling args in block On Thu, Oct 21, 2010 at 4:42 PM, salamond wrote: > Let's see if I get this. > While I'm passing @left_node, and @right_node into this block, > I'm passing the reference of these 2 objects into a block. > Inside block, I can use these 2 references to modify the objects, > but I can't change the reference themselves. > > Now here comes an update, is there a way to have the class destroy itself? > > like > class Node > #snip >    def destroy >        self = nil >    end > end > > [@left_node, @right_node].each do |node| >    node.destroy > end > > Is it possible to have something like this work? > When I try this, it said can't change the value of self. I think you are confused about how variables and objects work in Ruby. Variables (local or instance) point to objects. You use a variable to send messages to the object it references. You can have many variables pointing to the same object, and an object doesn't know which variables pointing to itself exist. So, @left_node and node are two such variables that, in the first iteration of the loop, are pointing to the same object. You can use the variable node to send messages to the object, but that can never affect which is object is referenced by @left, it can only modify the internal state of the object. An object cannot destroy itself. The only way to destroy an object is to remove all references to it, and eventually the garbage collector will take care of it. So, what you need to do here is to change the @left variable, making it point to nil, so that the original object is not pointed to anymore. If you have a collection of child nodes, then do as a I explained before: delete from the array the chosen objects so they are not pointed from the array anymore. Jesus.