From: Robert Evans Date: 2006-11-27T06:49:44+09:00 Subject: REXML and Date interaction Hi, I have a problem parsing dates from XML. I have this little program which produces an error, and I was able to fix it by moving the date parsing into a class that is distinct from the xml processing. Below is a broken version and the error it prints, and then a working version with the correct output. I would be grateful if anyone could explain what is going wrong here. It looks like it turns the class Date into a string Constant. Thanks, Bob Evans Broken Version =========== #!/usr/bin/env ruby require 'rexml/document' require 'date' class DateFun def parse(xml) doc = REXML::Document.new(xml) elems = doc.root.get_elements('//body/dateelement[@date]') elems.map { |s| s.attributes["date"].to_s }.sort end def sorted_dates_from(xml) sorted_dates = parse(xml) sorted_dates.map { |d| Date.parse(d)}.sort end end xmlDates = < HERE sorted_dates = DateFun.new.sorted_dates_from(xmlDates) sorted_dates.each { |d| puts d.to_s} Produces =========== NoMethodError: undefined method `parse' for "2005/224":String from /Users/bobevans/Documents/projects/rubyplay/brokendate.rb:18:in `sorted_dates_from' from /Users/bobevans/Documents/projects/rubyplay/brokendate.rb:18:in `sorted_dates_from' from /Users/bobevans/Documents/projects/rubyplay/brokendate.rb:26 from (irb):1835 from /usr/local/lib/ruby/1.8/rexml/element.rb:1182 Working Version ============ #!/usr/bin/env ruby require 'rexml/document' require 'date' class DateFun include REXML def parse(xml) doc = Document.new(xml) elems = doc.root.get_elements('//body/dateelement[@date]') elems.map { |s| s.attributes["date"].to_s }.sort end end class Other def sorted_dates_from(xml) sorted_dates = DateFun.new.parse(xml) sorted_dates.map { |d| Date.parse(d)}.sort end end xmlDates = < HERE sorted_dates = Other.new.sorted_dates_from(xmlDates) sorted_dates.each { |d| puts d.to_s} Produces: 1954-02-05 1970-02-06 1973-12-28