[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Scope...

Wood, Jeff

11/15/2004 6:06:00 PM

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.


1 Answer

Robert Klemme

11/15/2004 8:09:00 PM

0


"Jeff Wood" <jeffwood@amazon.com> 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