From: Robert Klemme Date: 2009-08-25T16:24:33+09:00 Subject: Re: Stuck on silly nil issue Nick, please do not top post. 2009/8/25 Nick Wild : > Robert - Thanks for taking a look! > > Maybe I should have used my actual code instead of trying to write up an > example. The lack of end on my if in the example wasn't the issue. I actually meant the use of a constant vs. the local variable from the iteration: puts cars["yugo"] ... > Here's the actual block with content sanitized. > > Where xmldoc is a REXML object I'm using without problem elsewhere in > this code. > > The IF line below is where I hit the problems > > types = ["foo", "bar", "stuff", "other", "favorite"] > conditions = ["up", "down", "marginal"] > > > types.each { |@scan_type| >  conditions.each { |@scan_conditions| >   if xmldoc.elements[@scan_type].attributes[@scan_conditions] >    count_to_add = > site.elements[@scan_type].attributes[@scan_conditions].to_i >    addtodb(siteToSave.id, scan_type, scan_conditions,count_to_add) >   end >  } > } You are abusing instance variables. Please get rid of all the @'s as they have no use in this case. Then, #elements returns a collection of elements - you need to iterate them if you want to get at each elements attributes. I believe though that there are XPath expressions which directly select attributes. I am unsure whether REXML supports them. See: http://www.w3schools.com/xpath/ http://www.zvon.org/xxl/XPathTutorial/General/examples.html My previous remark about not fetching things twice is even more valid with REXML. By refactoring to only fetch certain things once your code will become more readable and more efficient. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/