From: Robert Klemme Date: 2004-11-16T05:13:28+09:00 Subject: Re: Scope... "Jeff Wood" schrieb im Newsbeitrag news:4198F01F.5030204@amazon.com... > Ok, here's a snippet > > def parseElement( currentElement, currentNode ) > > # Step 1: Load the attributes from currentElement into currentNode > currentElement.attributes.each do |name, value| > currentNode.attributes[name] = value > end > # Step 2: Parse the children. > currentElement.children.each do |currentChild| > if currentChild.kind_of? Text > puts "Ignoring #{currentChild.value}" > end > if currentChild.kind_of? Element > > childNode = JWNode.new( currentChild.name ) > > ################################################################# > # PROBLEM HERE ... currentNode doesn't make it into this scope. # > # vvvvvvvvvvvv ################################################## > currentNode.children << childNode > > parseElement( currentChild, childNode ) > > end > end > > end > > The code is taking an REXML::Element tree ( xml doc ) ... and passing it > in with another object. > > As I try to add children to the passed in object, I find that currentNode > at the "PROBLEM HERE" point is Nil currentNode must be nil on invocation as there is no assignment and if...end does not open a new scope. If it would, you would get an exception for using an undefined variable IMHO. Or do you happen to have defined a method currentNode()? > I guess I don't understand Ruby's scoping logic as well as I thought I > did... help? I think you do but you are looking for the error in the wrong place. I'd put a "p currentNode" in the first line of the method and see what it prints. Kind regards robert