Wierd behaviour with Date

Discussion in 'Ruby' started by Damphyr, Sep 27, 2005.

  1. Damphyr

    Damphyr Guest

    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.
    Damphyr, Sep 27, 2005
    #1
    1. Advertising

  2. 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
    Robert Klemme, Sep 27, 2005
    #2
    1. Advertising

  3. Damphyr

    Damphyr Guest

    [REXML] Wierd behaviour with Date

    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::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.
    Damphyr, Sep 28, 2005
    #3
  4. Damphyr

    Damphyr Guest

    Re: [REXML] Wierd behaviour with Date

    --------------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::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--
    Damphyr, Sep 28, 2005
    #4
  5. Re: [REXML] Wierd behaviour with Date

    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
    Robert Klemme, Sep 28, 2005
    #5
  6. Damphyr

    Damphyr Guest

    Re: [REXML] Wierd behaviour with Date

    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::Date?
    Cheers,
    V.-

    ____________________________________________________________________
    http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
    http://www.freemail.gr - free email service for the Greek-speaking.
    Damphyr, Sep 28, 2005
    #6
  7. Re: [REXML] Wierd behaviour with Date

    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::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
    Robert Klemme, Sep 28, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon

    Wierd DataGrid behaviour

    Simon, Nov 5, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    361
    Simon
    Nov 5, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,229
    Michael Borgwardt
    May 30, 2004
  3. PSN
    Replies:
    3
    Views:
    366
  4. Vaddina Prakash Rao

    Wierd behaviour of function 'strcspn'

    Vaddina Prakash Rao, Dec 17, 2004, in forum: C Programming
    Replies:
    2
    Views:
    335
    Dave Thompson
    Dec 20, 2004
  5. SiWi
    Replies:
    3
    Views:
    806
    geremy condra
    Dec 9, 2009
Loading...

Share This Page