On 10/20/2010 9:16 AM, salamond wrote:
> Hi, guys.
> 
> I'm writing a Node class for a Binary Tree.
> Coding comes below.
> 
> There's a method remove_leaf.
> Now I want to do it this way:
> =begin
>                        [@left_node, @right_node].each do |node|
>                                if node.is_leaf?
>                                        node = nil
>                                end
>                        end
> =end
> 
> But it doesn't work.
> Is there a way to do it ?

In your example, node is just a reference to the same objects referenced
by @left_node and @right_node.  Assigning nil to node only discards the
reference held by node.  It looks like you're trying to do this too much
the C/C++ way, where you would have a pointer to a pointer that you
could then nullify.

The alternative code you're already using (where you unrolled this loop)
is probably the easiest and most straightforward way to deal with this
issue.

-Jeremy