1

I love the pluralize method in Rails. I'm hoping there's a similar mechanism for fractional numbers where they can be more human readable. For example, this would be ideal:

> include ActionView::Helpers::TextHelper
> pluralize(2.0, 'donut')
 => "2.0 donuts" 
> pluralize_and_humanize(2.0, 'donut')
 => "2 donuts"
> pluralize_and_humanize(2.5, 'donut')
 => "2 and a half donuts"
> pluralize_and_humanize(1.0, 'donut')
 => "1 donut"
> pluralize_and_humanize(0.5, 'donut')
 => "half a donut"
> pluralize_and_humanize(0.75, 'donut')
 => "3/4 of a donut"

Anything built in to rails? Any gems I can use?

at.
  • 50,922
  • 104
  • 292
  • 461
  • I was curious so I went and looked around and I don't think anything of this sort exists. This is the only project I could find which is vaguely related: https://github.com/aj0strow/ruby-human-fractions. See also https://github.com/search?l=Ruby&q=fractions&source=c&type=Repositories – Carlos Drew Sep 11 '13 at 21:32

1 Answers1

1

Actually, I think I have something for you.

Set up the rails fractions plugin in your project. Then, write your own helper, like so: (pseudocode)

def pluralize_and_humanize(number, string)
  split number into integer and fractional part -> whole, fraction

  convert fraction to [nice_fraction][2] if there is a fractional component

  output whole number + nice_fraction + pluralized string
end

Now, I'm not helping you with outputting "and a half" or "and a quarter" but I think '2 1/2' would at least be better than 2.5.

Carlos Drew
  • 1,633
  • 9
  • 17