Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

distance_of_time_in_words displays Rational instead of integer

#3806

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.

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

State: open
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Rohit Arondekar
    Rohit Arondekar
    • 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.

    October 7th, 2010 @ 11:37 AM

  2. Corey
    Corey

    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.

    October 8th, 2010 @ 02:59 AM

  3. Rohit Arondekar
    Rohit Arondekar
    • 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!

    October 8th, 2010 @ 03:07 AM

  4. Corey
    Corey

    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.

    October 8th, 2010 @ 03:34 AM

  5. Ryan Bigg
    Ryan Bigg

    Automatic cleanup of spam.

    October 9th, 2010 @ 09:57 PM