From: Tom Pollard Date: 2006-12-31T00:41:37+09:00 Subject: Re: Creating New Objects from REXML::Elements On Dec 22, 2006, at 1:20 AM, eddieroger wrote: > Tom Pollard wrote: >> On Dec 21, 2006, at 1:40 AM, eddieroger wrote: >>> [...] when I try to push my new objects into an array, >>> the class identifies itself as an REXML::Element instead of a Movie. >>> [...] >>> irb tells me this: >>> irb(main):025:0> movies.class >>> => Array >>> irb(main):026:0> movies[1].class >>> => REXML::Element >> >> I've encountered precisely this same problem! I use REXML to pull >> values out of an XML document and into an object; but, instead of my >> objects, I end up with an Array of REXML::Element's. :-( > > Have you solved this? Finally. I realized I'd made the bone-headed mistake of not specifying an explicit return value in my function that processed the REXML fragment. What I had was class Evcase def from_rexml ( record ) record.each_element do |el| begin self.send("ev_#{el.name.downcase}=", el.text) rescue Evcase.create_accessors(el.name.downcase) retry end end end end This was used from code that looked like doc.root.get_elements('PROBLEM_RECORD').each do |rec| @cases << Evcase.new.from_rexml(rec) end This latter code depends on the Evcase object being returned from the _from_rexml method; instead, it was returning the REXML element ('record') that had been passed in. I'm not sure why it took me so long to see that. Hope this helps someone else... TomP