Variables in REXML

5

54Sandgroper

Real nuby question.

This works fine...
irb> puts root.elements['foo/bar[3]'].text
three

I can't work out how to do this...
irb> i = 3
irb> puts root.elements['foo/bar'].text

I keep getting error message
NoMethodError: undefined method `text' for nil:NilClass

I've tried defining "i" every which way -- , [@i] and others.

irb> $i = 3
irb> [${i}]
doesn't error, but it outputs as if i = 1.

All of the REXML examples for text nodes that I have been able to track
down have the index hard coded, or use .each

Can someone point me in the right direction?
 
P

Paul Battley

SSB0aGluayB0aGlzIGlzIHRoZSBzdHJpbmcgaW50ZXJwb2xhdGlvbiB5b3UncmUgbG9va2luZyBm
b3I6CgogIHJvb3QuZWxlbWVudHNbImZvby9iYXJbI3tpfV0iXS50ZXh0CgpQYXVsLgo=
 
5

54Sandgroper

I can't work out how to do this...
irb> i = 3
irb> puts root.elements['foo/bar'].text


Please try:
puts root.elements["foo/bar[#{i}]"].text


Aha! The double quotes did the trick.

I had unsucessfully tried
puts root.elements['foo/bar[#{i}]'].text

Thanks a million!
 

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

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,668
Latest member
SamiraShac

Latest Threads

Top