# Is there a way to AutoParse a string to another type - e.g. if a Date format then date, else if inte

**URL:** <https://www.ruby-forum.com/t/is-there-a-way-to-autoparse-a-string-to-another-type-e-g-if-a-date-format-then-date-else-if-inte/145120>\
**Category:** Ruby\
**Created:** [August 6, 2008, 6:56am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-autoparse-a-string-to-another-type-e-g-if-a-date-format-then-date-else-if-inte/145120 "2008-08-06T06:56:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [August 6, 2008, 6:56am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-autoparse-a-string-to-another-type-e-g-if-a-date-format-then-date-else-if-inte/145120/1 "2008-08-06T06:56:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [August 6, 2008, 7:19am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-autoparse-a-string-to-another-type-e-g-if-a-date-format-then-date-else-if-inte/145120/2 "2008-08-06T07:19:06Z")

</div>

On Aug 5, 2008, at 10:53 PM, Greg H. 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/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [August 6, 2008, 7:27am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-autoparse-a-string-to-another-type-e-g-if-a-date-format-then-date-else-if-inte/145120/3 "2008-08-06T07:27:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [August 6, 2008, 7:53am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-autoparse-a-string-to-another-type-e-g-if-a-date-format-then-date-else-if-inte/145120/4 "2008-08-06T07:53:01Z")

</div>

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
> 
> >

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [August 6, 2008, 6:55pm UTC](https://www.ruby-forum.com/t/is-there-a-way-to-autoparse-a-string-to-another-type-e-g-if-a-date-format-then-date-else-if-inte/145120/5 "2008-08-06T18:55:09Z")

</div>

On Aug 5, 2008, at 11:50 PM, Greg H. 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/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [August 6, 2008, 3:55pm UTC](https://www.ruby-forum.com/t/is-there-a-way-to-autoparse-a-string-to-another-type-e-g-if-a-date-format-then-date-else-if-inte/145120/6 "2008-08-06T15:55:50Z")

</div>

On Wed, Aug 6, 2008 at 12:50 AM, Greg H.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [August 6, 2008, 3:52pm UTC](https://www.ruby-forum.com/t/is-there-a-way-to-autoparse-a-string-to-another-type-e-g-if-a-date-format-then-date-else-if-inte/145120/7 "2008-08-06T15:52:54Z")

</div>

On Wed, Aug 6, 2008 at 1:50 AM, Greg H.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 G.
