# Still trying to coerce numbers and strings for date math

**URL:** <https://www.ruby-forum.com/t/still-trying-to-coerce-numbers-and-strings-for-date-math/130246>\
**Category:** Rails\
**Created:** [February 26, 2008, 6:59pm UTC](https://www.ruby-forum.com/t/still-trying-to-coerce-numbers-and-strings-for-date-math/130246 "2008-02-26T18:59:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 26, 2008, 6:59pm UTC](https://www.ruby-forum.com/t/still-trying-to-coerce-numbers-and-strings-for-date-math/130246/1 "2008-02-26T18:59:06Z")

</div>

and still going crazy…

I have tried everything I can think of…

> > interval  
> > =\> 1
> 
> > dinc  
> > =\> “year”
> 
> > interval.to\_s + “.” + dinc  
> > =\> “1.year”
> 
> > test = Time.now + (interval.to\_s + “.” + dinc)  
> > TypeError: no implicit conversion to float from string  
> > from (irb):263:in `+’  
> > from (irb):263  
> > from :0

crap…that doesn’t work, nor does this…

> > (interv.to\_s + “.” + dinc).to\_f  
> > =\> 1.0

I have to have ‘interval’ be a number that I can increment while I test  
it and ‘dinc’ to be the value (i.e., month, year) and reconstitute them.

How does one do this?

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 26, 2008, 7:05pm UTC](https://www.ruby-forum.com/t/still-trying-to-coerce-numbers-and-strings-for-date-math/130246/2 "2008-02-26T19:05:05Z")

</div>

On 26 Feb 2008, at 17:58, Craig W. wrote:

> =\> “1.year”
> 
> I have to have ‘interval’ be a number that I can increment while I  
> test  
> it and ‘dinc’ to be the value (i.e., month, year) and reconstitute  
> them.

I suppose the key here is understanding that year/month etc… are  
just methods on the integers, so  
interval = 1  
inc = ‘year’  
test = Time.now + interval.year  
test = Time.now + interval.send(inc)

Fred

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 26, 2008, 7:18pm UTC](https://www.ruby-forum.com/t/still-trying-to-coerce-numbers-and-strings-for-date-math/130246/3 "2008-02-26T19:18:01Z")

</div>

On Tue, 2008-02-26 at 18:03 +0000, Frederick C. wrote:

> > > > dinc
> 
> interval = 1  
> inc = ‘year’  
> test = Time.now + interval.year  
> test = Time.now + interval.send(inc)

* * *

bing!

Thanks, I don’t know that I would have ever figured this out by myself.

Craig
