Wierd behaviour with Date

D

Damphyr

Tell me where does everything get mixed-up

site.rb:167:in `generate': undefined method `strptime' for
"+2004/200":String (NoMethodError)

The offending line is

date_i=Date.strptime(date_txt,"%d.%m.%Y")

How can Date be "+2004/200"
?
On irb everything is OK.
Cheers,
V.-

____________________________________________________________________
http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
http://www.freemail.gr - free email service for the Greek-speaking.
 
R

Robert Klemme

Damphyr said:
Tell me where does everything get mixed-up

site.rb:167:in `generate': undefined method `strptime' for
"+2004/200":String (NoMethodError)

The offending line is

date_i=Date.strptime(date_txt,"%d.%m.%Y")

How can Date be "+2004/200"
?

Date = "+2004/200"

As easy as that... :)

This works:
=> #<Date: 4906577/2,0,2299161>

What did you do?

robert
 
D

Damphyr

Robert said:
Date = "+2004/200"

As easy as that... :)

This works:

=> true


=> "10.10.2004"


=> #<Date: 4906577/2,0,2299161>

What did you do?
That's my question. And I think I found it:
If I do
class Article
include REXML
...
...
end

I get the error with strptime.

If I use REXML::Document and don't include, then everything is fine.
So REXML does something to Date.
I run
ruby 1.8.2 (2004-12-25) [i386-mswin32]

Cheers,
V.-

____________________________________________________________________
http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
http://www.freemail.gr - free email service for the Greek-speaking.
 
D

Damphyr

--------------020808070807060205090301
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Robert Klemme wrote:
snip
=> #<Date: 4906577/2,0,2299161>

What did you do?
That's my question. And I think I found it: If I do class Article
include REXML ... ... end

I get the error with strptime.

If I use REXML::Document and don't include, then everything is fine.
So REXML does something to Date.
I run ruby 1.8.2 (2004-12-25) [i386-mswin32]

Since I cannot duplicate the problem in irb with a simple include,
here's the code that duplicates this, maybe someone can see what's wrong.
Cheers,
V.-



____________________________________________________________________
http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
http://www.freemail.gr - free email service for the Greek-speaking.
--------------020808070807060205090301
Content-Type: text/xml;name="example.xml"
Content-Disposition: inline;filename="example.xml"
Content-Transfer-Encoding: quoted-printable

=EF=BB=BF<article>
<title>Title</title>
<date>10.05.06</date>
<language>el</language>
<text>Some text in UTF8, =CF=87=CE=B5=CF=87=CE=B5=CF=87=CE=AD</text>
</article>
--------------020808070807060205090301
Content-Type: text/plain;name="test.rb"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;filename="test.rb"

require 'rexml/document'
class Article
include REXML
ITEM_TITLE="title"
ITEM_CREATION="date"
ITEM_TEXT="text"
ITEM_LASTMODIFIED="lastmodified"
attr_reader :language,:text,:title,:created,:section,:filename,:last_modified
attr_writer :section
def initialize title,filename,text,creation_date,lastmodified="",language="el"
@title=title
@filename=filename
@text=text
@created=creation_date
@last_modified=last_modified
@language=language
@section=""
end

def to_s
return "#{@created} - #{@title}"
end
#Creates an Article instance from an XML file
#
# Raises BadArticleException if the file is malformed
def Article.generate filename
begin
xmldoc=Document.new( File.new(filename) )
filename=File.basename(filename).chomp(".xml")
title=xmldoc.root.elements[ITEM_TITLE].collect{|el|
el.to_s
}.join
date_txt=xmldoc.root.elements[ITEM_CREATION].text
#convert the date
begin
created=Date.strptime(date_txt,"%d.%m.%Y")
rescue ArgumentError
end
text=xmldoc.root.elements[ITEM_TEXT].collect{|el|
el.to_s
}.join
language="el"
lastmodified=created
begin
lastmodified_txt=xmldoc.root.elements[ITEM_LASTMODIFIED].text
if lastmodified_txt
begin
lastmodified=Date.strptime(lastmodified_txt,"%d.%m.%Y")
rescue ArgumentError
lastmodified=Date.new
end
end
end if xmldoc.root.elements[ITEM_LASTMODIFIED]
return Article.new(title,filename,text,created,lastmodified,language)
#rescue
# raise BadArticleException.new(filename,$!)
end
end
end

puts Article.generate("example.xml")
--------------020808070807060205090301--
 
R

Robert Klemme

You should not include REXML:

12:17:39 [ruby]: ruby -r date -e 'p Date.object_id; require
"rexml/document"; p Date.object_id; class X; include REXML; p
Date.object_id; end'
135087764
135087764
135679456
12:17:51 [ruby]:

Notice the different last id?

Kind regards

robert
 
D

Damphyr

Robert said:
You should not include REXML:

12:17:39 [ruby]: ruby -r date -e 'p Date.object_id; require
"rexml/document"; p Date.object_id; class X; include REXML; p
Date.object_id; end' 135087764 135087764 135679456 12:17:51 [ruby]:

Notice the different last id?
Well, I figuredd that out and I run my code without the include, but the
question remains, what does REXML do to Date?
'Should not include REXML when using Date' is not something that one
expects, is it?
Anyway, can I call Date instead of REXML::Date?
Cheers,
V.-

____________________________________________________________________
http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
http://www.freemail.gr - free email service for the Greek-speaking.
 
R

Robert Klemme

Damphyr said:
Robert said:
You should not include REXML:

12:17:39 [ruby]: ruby -r date -e 'p Date.object_id; require
"rexml/document"; p Date.object_id; class X; include REXML; p
Date.object_id; end' 135087764 135087764 135679456 12:17:51 [ruby]:

Notice the different last id?
Well, I figuredd that out and I run my code without the include, but
the question remains, what does REXML do to Date?

IMHO REXML is not intended for inclusion it's just a namespace module. If
you import it you get all sorts of stuff inside that module into your
class. By importing it you create the effect that the namespace was
introduced to avoid.
'Should not include REXML when using Date' is not something that one
expects, is it?
Anyway, can I call Date instead of REXML::Date?

::Date

14:23:55 [ruby]: ruby -r date -e 'p Date.object_id; require
"rexml/document"; p Date.object_id; class X; include REXML; p
Date.object_id; p ::Date.ob
ject_id; end'
135087828
135087828
135677044
135087828

Kind regards

robert
 

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

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top