Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > is there a way to AutoParse a string to another type - e.g. if aDate format then date, else if integer than Integer etc ?????

Reply
Thread Tools

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

 
 
Greg Hauptmann
Guest
Posts: n/a
 
      08-06-2008
[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

 
Reply With Quote
 
 
 
 
ara.t.howard
Guest
Posts: n/a
 
      08-06-2008

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




 
Reply With Quote
 
 
 
 
Greg Hauptmann
Guest
Posts: n/a
 
      08-06-2008
[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 <(E-Mail Removed)> 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
>
>
>
>
>


 
Reply With Quote
 
Greg Hauptmann
Guest
Posts: n/a
 
      08-06-2008
[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 <(E-Mail Removed)> 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 <(E-Mail Removed)> 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
> >
> >
> >
> >
> >

>


 
Reply With Quote
 
Michael Guterl
Guest
Posts: n/a
 
      08-06-2008
On Wed, Aug 6, 2008 at 1:50 AM, Greg Hauptmann
<(E-Mail Removed)> 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

 
Reply With Quote
 
Michael Libby
Guest
Posts: n/a
 
      08-06-2008
On Wed, Aug 6, 2008 at 12:50 AM, Greg Hauptmann
<(E-Mail Removed)> 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

 
Reply With Quote
 
ara.t.howard
Guest
Posts: n/a
 
      08-06-2008

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




 
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
XML-Schema: Is there a way to make different type elements(each type has more than one Element) present in random order? Aray XML 1 01-05-2007 08:22 AM
If / Then / Else Help Needed - More then one condition possible? tbird2340@gmail.com Javascript 15 09-21-2006 01:27 PM
Since there was talk of if-then-else not being allowed in lambda expressions, the following is from "Dive into Python" Casey Hawthorne Python 6 07-21-2006 05:30 PM
How does one play a .wav file and then another and then another...? ted.j.conway@gmail.com Javascript 4 01-17-2006 03:28 AM
Help. SessionID is x then y then x then y BodiKlamph@gmail.com ASP General 0 09-03-2005 03:02 PM



Advertisments