# Weird results i'm getting please explain

**URL:** <https://www.ruby-forum.com/t/weird-results-im-getting-please-explain/105787>\
**Category:** Rails\
**Created:** [June 2, 2007, 7:15pm UTC](https://www.ruby-forum.com/t/weird-results-im-getting-please-explain/105787 "2007-06-02T19:15:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [June 2, 2007, 7:15pm UTC](https://www.ruby-forum.com/t/weird-results-im-getting-please-explain/105787/1 "2007-06-02T19:15:30Z")

</div>

Hi,

This is kind of weird thing, I cannot explain it, but I will show you my  
code - this would explain it better.

I have a helper method:

def month\_in\_words(month)  
logger.debug(month)  
Time.parse(month).strftime("%b")  
end

and in my view I have:

\<% @array.each do | ar | %\>  
\<%= month\_in\_words(ar) %\>  
\<% end %\>

The result I’m getting in the view is the same for every call to helper  
method month\_in\_words even if it’s getting different value in the  
parameter?

View:

> Jun  
> Jun  
> Jun  
> Jun

Debugger (from ruby script/server console):

> 06  
> 05  
> 04  
> 03

Why do I keep getting Jun, is this a error within RubyonRails or is it  
something I did not understand yet?

Please explain 🙂

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [June 2, 2007, 7:50pm UTC](https://www.ruby-forum.com/t/weird-results-im-getting-please-explain/105787/2 "2007-06-02T19:50:21Z")

</div>

Jamal,

Your code does explain what’s going on. Time.parse is not returning  
what you would expect because you are passing in just the String  
representation of a month (rather than of a complete date). An easy  
way to make it do what you want is to tack on a day to the end of the  
string.

For instance (in IRB):

> > Time.parse(“03”)  
> > =\> Sat Jun 02 12:46:40 CDT 2007
> 
> > Time.parse(“04”)  
> > =\> Sat Jun 02 12:46:44 CDT 2007
> 
> > Time.parse(“03/01”)  
> > =\> Thu Mar 01 00:00:00 CST 2007
> 
> > Time.parse(“04/01”)  
> > =\> Sun Apr 01 00:00:00 CDT 2007
> 
> > Time.parse(“04/01”).strftime("%b")  
> > =\> “Apr”

I hope this helps,

David

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [June 2, 2007, 8:59pm UTC](https://www.ruby-forum.com/t/weird-results-im-getting-please-explain/105787/3 "2007-06-02T20:59:32Z")

</div>

David Altenburg wrote:

> Jamal,
> 
> Your code does explain what’s going on. Time.parse is not returning  
> what you would expect because you are passing in just the String  
> representation of a month (rather than of a complete date). An easy  
> way to make it do what you want is to tack on a day to the end of the  
> string.
> 
> For instance (in IRB):
> 
> > > Time.parse(“03”)  
> > > =\> Sat Jun 02 12:46:40 CDT 2007
> > 
> > > Time.parse(“04”)  
> > > =\> Sat Jun 02 12:46:44 CDT 2007
> > 
> > > Time.parse(“03/01”)  
> > > =\> Thu Mar 01 00:00:00 CST 2007
> > 
> > > Time.parse(“04/01”)  
> > > =\> Sun Apr 01 00:00:00 CDT 2007
> > 
> > > Time.parse(“04/01”).strftime("%b")  
> > > =\> “Apr”
> 
> I hope this helps,
> 
> David

Thanks, That kind of weird, I just added a year and it helps. Why is  
that? Since I’m passing it different month number as a string, shouldn’t  
it look at them as separate numbers as strings?

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [June 3, 2007, 1:11am UTC](https://www.ruby-forum.com/t/weird-results-im-getting-please-explain/105787/4 "2007-06-03T01:11:55Z")

</div>

On Jun 2, 2007, at 1:59 PM, Jamal S. wrote:

> > For instance (in IRB):  
> > =\> “Apr”
> > 
> > I hope this helps,
> > 
> > David
> 
> Thanks, That kind of weird, I just added a year and it helps. Why is  
> that? Since I’m passing it different month number as a string,  
> shouldn’t  
> it look at them as separate numbers as strings?

Well, the problem is that when you only pass Time.parse a single  
number, it does not know how to interpret that. Is it a month? A  
year? A day? There’s no real way to tell. The way Time.parse works is  
it tries to match the string you pass in against a series of  
patterns. Most of the time, it does what you want. (i.e., if a person  
could tell how to interpret the string as a date, Time.parse probably  
will be able to as well). The exact details of how it does the  
matching, however, are complex. The best place to look is probably  
the source code – see the \_parse method in date/format.rb in the  
standard libs if you want to see exactly what it’s doing – there’s a  
lot of code there, but it’s pretty readable, as long as you grok  
regular expressions.

Thanks,

David L Altenburg  
[http://gensym.org](http://gensym.org)
