is there a way to AutoParse a string to another type - e.g. if aDate format then date, else if integ

Discussion in 'Ruby' started by Greg Hauptmann, Aug 6, 2008.

  1. [Note: parts of this message were removed to make it a legal post.]

    Hi,

    Is there a way to AutoParse a string to another type - e.g. if a Date format
    then date, else if integer than Integer etc

    I'm wanting to pass search parameters via a URL, however since everything in
    the URL will be a string it would be good to automatically convert a string
    to the most likely variable type. In particular differentiating between an
    Integer and a Date.

    Does this exist somewhere in Ruby already? Or do I have to write myself?

    Thanks in advance
     
    Greg Hauptmann, Aug 6, 2008
    #1
    1. Advertising

  2. Greg Hauptmann

    ara.t.howard Guest

    On Aug 5, 2008, at 10:53 PM, Greg Hauptmann wrote:

    > Does this exist somewhere in Ruby already? Or do I have to write
    > myself?



    require 'date'

    value = Date.parse(string) rescue Integer(string)




    a @ http://codeforpeople.com/
    --
    we can deny everything, except that we have the possibility of being
    better. simply reflect on that.
    h.h. the 14th dalai lama
     
    ara.t.howard, Aug 6, 2008
    #2
    1. Advertising

  3. [Note: parts of this message were removed to make it a legal post.]

    thanks - in one line too....so I could do in fact then I guess:

    value = Date.parse(string) rescue Integer(string) rescue Float(string)
    rescue string




    On 8/6/08, ara.t.howard <> wrote:
    >
    >
    > On Aug 5, 2008, at 10:53 PM, Greg Hauptmann wrote:
    >
    > Does this exist somewhere in Ruby already? Or do I have to write myself?
    >>

    >
    >
    > require 'date'
    >
    > value = Date.parse(string) rescue Integer(string)
    >
    >
    >
    >
    > a @ http://codeforpeople.com/
    > --
    > we can deny everything, except that we have the possibility of being
    > better. simply reflect on that.
    > h.h. the 14th dalai lama
    >
    >
    >
    >
    >
     
    Greg Hauptmann, Aug 6, 2008
    #3
  4. [Note: parts of this message were removed to make it a legal post.]

    umm...Date parse doesn't seem to be that good, for example see below

    >> Loading development environment (Rails 2.1.0)
    >>

    ?> Date.parse("123", "%d/%m/%Y")
    => Fri, 02 May 2008
    >> Date.parse("123", "%d/%m/%Y").class

    => Date
    >>



    On 8/6/08, Greg Hauptmann <> wrote:
    >
    > thanks - in one line too....so I could do in fact then I guess:
    >
    > value = Date.parse(string) rescue Integer(string) rescue Float(string)
    > rescue string
    >
    >
    >
    >
    > On 8/6/08, ara.t.howard <> wrote:
    > >
    > >
    > > On Aug 5, 2008, at 10:53 PM, Greg Hauptmann wrote:
    > >
    > > Does this exist somewhere in Ruby already? Or do I have to write myself?
    > >>

    > >
    > >
    > > require 'date'
    > >
    > > value = Date.parse(string) rescue Integer(string)
    > >
    > >
    > >
    > >
    > > a @ http://codeforpeople.com/
    > > --
    > > we can deny everything, except that we have the possibility of being
    > > better. simply reflect on that.
    > > h.h. the 14th dalai lama
    > >
    > >
    > >
    > >
    > >

    >
     
    Greg Hauptmann, Aug 6, 2008
    #4
  5. On Wed, Aug 6, 2008 at 1:50 AM, Greg Hauptmann
    <> wrote:
    > umm...Date parse doesn't seem to be that good, for example see below
    >
    >>> Loading development environment (Rails 2.1.0)
    >>>

    > ?> Date.parse("123", "%d/%m/%Y")
    > => Fri, 02 May 2008
    >>> Date.parse("123", "%d/%m/%Y").class

    > => Date
    >>>

    >


    FWIW, it is assuming you want the 123rd day of the year.

    Michael Guterl
     
    Michael Guterl, Aug 6, 2008
    #5
  6. On Wed, Aug 6, 2008 at 12:50 AM, Greg Hauptmann
    <> wrote:
    > umm...Date parse doesn't seem to be that good, for example see below
    >
    >>> Loading development environment (Rails 2.1.0)
    >>>

    > ?> Date.parse("123", "%d/%m/%Y")
    > => Fri, 02 May 2008
    >>> Date.parse("123", "%d/%m/%Y").class

    > => Date


    As far as I can tell, Date.parse doesn't really want a format string
    as the second argument. And Date.parse is working great in your
    example, it gave you a date corresponding to the 123rd day of this
    year. :)

    If you're not sure whether your input string is actually a date, you
    might be more interested in Date.strptime:

    irb(main):012:0> value = Date.strptime("123", "%d/%m/%y") rescue Integer("123")
    => 123
    irb(main):013:0> value.class
    => Fixnum

    -Michael
     
    Michael Libby, Aug 6, 2008
    #6
  7. Greg Hauptmann

    ara.t.howard Guest

    On Aug 5, 2008, at 11:50 PM, Greg Hauptmann wrote:

    > umm...Date parse doesn't seem to be that good, for example see below


    yup - you may have to constrain what you parse with a pattern. you
    might also try Time.parse as it's a little more strict.

    a @ http://codeforpeople.com/
    --
    we can deny everything, except that we have the possibility of being
    better. simply reflect on that.
    h.h. the 14th dalai lama
     
    ara.t.howard, Aug 6, 2008
    #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. Brian Candy
    Replies:
    2
    Views:
    1,174
    Janaka
    Feb 18, 2004
  2. Stylus Studio
    Replies:
    0
    Views:
    532
    Stylus Studio
    Oct 5, 2004
  3. jason

    Handling Ref. Integ Error?

    jason, Aug 15, 2003, in forum: ASP General
    Replies:
    6
    Views:
    127
    Aaron Bertrand - MVP
    Aug 15, 2003
  4. Replies:
    4
    Views:
    547
    cwdjrxyz
    Jan 17, 2006
  5. Replies:
    15
    Views:
    227
Loading...

Share This Page