# REXML and Date interaction

**URL:** <https://www.ruby-forum.com/t/rexml-and-date-interaction/77320>\
**Category:** Ruby\
**Created:** [November 26, 2006, 10:51pm UTC](https://www.ruby-forum.com/t/rexml-and-date-interaction/77320 "2006-11-26T22:51:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![revans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@revans](https://www.ruby-forum.com/u/revans)\
**Post date:** [November 26, 2006, 10:51pm UTC](https://www.ruby-forum.com/t/rexml-and-date-interaction/77320/1 "2006-11-26T22:51:48Z")

</div>

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 E.

# 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

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

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

---

<div class="post-metadata">

**Author:** ![revans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@revans](https://www.ruby-forum.com/u/revans)\
**Post date:** [November 26, 2006, 10:55pm UTC](https://www.ruby-forum.com/t/rexml-and-date-interaction/77320/2 "2006-11-26T22:55:50Z")

</div>

Hi –

On Mon, 27 Nov 2006, Robert E. wrote:

> 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.

Your broken version worked for me. What version of Ruby are you  
using?

David

---

<div class="post-metadata">

**Author:** ![revans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@revans](https://www.ruby-forum.com/u/revans)\
**Post date:** [November 26, 2006, 11:13pm UTC](https://www.ruby-forum.com/t/rexml-and-date-interaction/77320/3 "2006-11-26T23:13:51Z")

</div>

Hi,

Thanks for running it, indeed that broken version works, and I  
must’ve corrupted my irb session. So, I tried it again and discovered  
more precisely the problem. If I include REXML in the class instead  
of explicitly specifying REXML::Document, then I get the problem.  
Which, I can just “not do that” for now, but it is still curious what  
is happening. Here is a version that seems to produce the error still.

Thanks for the sanity check,

Bob E.

# Better Broken

#!/usr/bin/env ruby

require ‘rexml/document’  
require ‘date’

class DateFun

## BEGIN Different

include REXML

## END Different

def parse(xml)

## BEGIN Different

```
 doc = Document.new(xml)

```

##End Different

```
 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

HERE

sorted\_dates = DateFun.new.sorted\_dates\_from(xmlDates)  
sorted\_dates.each { |d| puts d.to\_s}

# Produces

./brokendate.rb  
./brokendate.rb:18:in `sorted_dates_from': undefined method`parse’  
for “2005/224”:String (NoMethodError)  
from ./brokendate.rb:18:in `sorted\_dates\_from’  
from ./brokendate.rb:26

---

<div class="post-metadata">

**Author:** ![revans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@revans](https://www.ruby-forum.com/u/revans)\
**Post date:** [November 26, 2006, 11:23pm UTC](https://www.ruby-forum.com/t/rexml-and-date-interaction/77320/4 "2006-11-26T23:23:58Z")

</div>

Hi –

On Mon, 27 Nov 2006, Robert E. wrote:

> 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

What’s happening is that you’re bringing REXML::Date into scope. You  
need to do:

```
::Date.parse

```

to get the top-level Date constant.

David

---

<div class="post-metadata">

**Author:** ![revans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@revans](https://www.ruby-forum.com/u/revans)\
**Post date:** [November 26, 2006, 11:30pm UTC](https://www.ruby-forum.com/t/rexml-and-date-interaction/77320/5 "2006-11-26T23:30:00Z")

</div>

On Nov 26, 2006, at 2:22 PM, removed\_email\_address@domain.invalid wrote:

> What’s happening is that you’re bringing REXML::Date into scope. You  
> need to do:
> 
> ::Date.parse
> 
> to get the top-level Date constant.
> 
> David

Ah cool. I didn’t realize there was a REXML::Date class, but I  
suspected it might be something like that, and I didn’t realize I  
could do just ::Date to fully qualify top level modules.

Thanks,  
Bob E.

---

<div class="post-metadata">

**Author:** ![revans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@revans](https://www.ruby-forum.com/u/revans)\
**Post date:** [November 26, 2006, 11:46pm UTC](https://www.ruby-forum.com/t/rexml-and-date-interaction/77320/6 "2006-11-26T23:46:02Z")

</div>

Hi –

On Mon, 27 Nov 2006, Robert E. wrote:

> > David
> 
> Ah cool. I didn’t realize there was a REXML::Date class, but I suspected it  
> might be something like that, and I didn’t realize I could do just ::Date to  
> fully qualify top level modules.

REXML::Date is actually a constant, but same idea.

David
