Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Wierd behaviour with Date

Reply
Thread Tools

Wierd behaviour with Date

 
 
Damphyr
Guest
Posts: n/a
 
      09-27-2005
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.


 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      09-27-2005
Damphyr wrote:
> 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:

>> require 'date'

=> true
>> date_txt = "10.10.2004"

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

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

What did you do?

robert

 
Reply With Quote
 
 
 
 
Damphyr
Guest
Posts: n/a
 
      09-28-2005
Robert Klemme wrote:
> Damphyr wrote:
>
>> 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:
>>> require 'date'

>
> => true
>
>>> date_txt = "10.10.2004"

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

>
> => #<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:ocument 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.


 
Reply With Quote
 
Damphyr
Guest
Posts: n/a
 
      09-28-2005
--------------020808070807060205090301
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Damphyr wrote:
> Robert Klemme wrote:
>

snip
>>
>>>> Date.strptime(date_txt,"%d.%m.%Y")

>>
>>
>> => #<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:ocument 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,lastmodifi ed,language)
#rescue
# raise BadArticleException.new(filename,$!)
end
end
end

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


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      09-28-2005
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

 
Reply With Quote
 
Damphyr
Guest
Posts: n/a
 
      09-28-2005
Robert Klemme wrote:
> 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:ate?
Cheers,
V.-

__________________________________________________ __________________
http://www.freemail.gr - δωρεάν υπηρεσία ηλεκτρονικού ταχυδρομείου.
http://www.freemail.gr - free email service for the Greek-speaking.


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      09-28-2005
Damphyr wrote:
> Robert Klemme wrote:
>> 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:ate?


:ate

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 :ate.ob
ject_id; end'
135087828
135087828
135677044
135087828

Kind regards

robert

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wierd power on behaviour tempus fugit Computer Information 5 01-15-2007 01:29 PM
Wierd Behaviour: Crash at 'delete' of memory. PSN C++ 3 03-09-2005 06:15 PM
Wierd behaviour of function 'strcspn' Vaddina Prakash Rao C Programming 2 12-20-2004 06:29 AM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Wierd DataGrid behaviour Simon ASP .Net 0 11-05-2003 05:40 AM



Advertisments