Scope...

J

Jeff Wood

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

I guess I don't understand Ruby's scoping logic as well as I thought I
did... help?

j.
 
R

Robert Klemme

Jeff Wood said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

scope puzzle 2
object scope and cleanup 4
Binding scope 10
scope when reopening method 3
HELP: Parsing XML fails under frames... 2
Include Schema Name When Creating XML 5
New Generator 1
Avoiding scope pitfall 2

Staff online

Members online

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top