This project is archived and is in readonly mode.

#3806 open
Corey

distance_of_time_in_words displays Rational instead of integer

Reported by Corey | January 28th, 2010 @ 05:26 PM

When using the mathn ruby standard library, either directly or indirectly, the display of distance_of_time_in_words for time distances of one year or greater shows a Rational number instead of an integer.

For example:

$ script/console 
Loading development environment (Rails 2.3.5)
>> from_time = Time.now
=> Thu Jan 28 09:11:53 -0800 2010
>> helper.distance_of_time_in_words(from_time, from_time + 1.year + 3.days)
=> "about 1 year"
>> require 'mathn'
=> ["Prime"]
>> helper.distance_of_time_in_words(from_time, from_time + 1.year + 3.days)
=> "about 368/365 years"
>>

I think that if line 92 in rails/actionpack/lib/action_view/helpers/date_helper.rb uses the round method on the result of the division for the distance_in_years value the display will be as expected.

Comments and changes to this ticket

  • Rohit Arondekar

    Rohit Arondekar October 7th, 2010 @ 11:37 AM

    • State changed from “new” to “stale”
    • Importance changed from “” to “Low”

    Marking ticket as stale. If this is still an issue please leave a comment with suggested changes, creating a patch with tests, rebasing an existing patch or just confirming the issue on a latest release or master/branches.

  • Corey

    Corey October 8th, 2010 @ 02:59 AM

    From the latest 2.3.x release:

        $ script/console 
        Loading development environment (Rails 2.3.9)
        >> from_time = Time.now
        => Thu Oct 07 18:20:30 -0700 2010
        >> helper.distance_of_time_in_words(from_time, from_time + 1.year + 3.days)
        => "about 1 year"
        >> require 'mathn'
        => ["Prime"]
        >> helper.distance_of_time_in_words(from_time, from_time + 1.year + 3.days)
        => "about 368/365 years"
        >>
    

    From the latest Rails release:

        $ rails console
        Loading development environment (Rails 3.0.0)
        >> from_time = Time.now
        => Thu Oct 07 18:28:08 -0700 2010
        >> helper.distance_of_time_in_words(from_time, from_time + 1.year + 3.days)
        => "about 1 year"
        >> require 'mathn'
        => ["Prime"]
        >> helper.distance_of_time_in_words(from_time, from_time + 1.year + 3.days)
        => "about 368/365 years"
        >>
    

    I wanted to add a test case that would illustrate the problem. However, as it turned out requiring 'mathn' in the date_helper_test.rb also had it loaded for all of the tests that run after it and some of those then also failed. I don't know how to sandbox the loading of 'mathn' in the test suite so that other tests aren't affected.

    However, for Fixnums the methods / and div give the same result, and that changing from / to div ensures that mathn's Rationals are not returned. I've created a patch from 2-3-stable which does that for distance_of_time_in_words and attached it.

  • Rohit Arondekar

    Rohit Arondekar October 8th, 2010 @ 03:07 AM

    • State changed from “stale” to “open”

    Wait, so mathn is redefining '/'? I think then it's the gems fault and not Rails. I mean Rails shouldn't change because of this. Still, opening ticket to get a consensus on this issue. Thanks for the reply!

  • Corey

    Corey October 8th, 2010 @ 03:34 AM

    Unfortunately, in my application I'm not loading 'mathn' directly, some gem I just happen to use is loading it. Everything was ok for me with until this change to the DateHelper.

    Just for reference, here's the Ruby issue related to mathn's behavior.

  • Ryan Bigg

    Ryan Bigg October 9th, 2010 @ 09:57 PM

    Automatic cleanup of spam.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Pages