Forum: Ruby on Rails Conditional pluralize without the number

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B15edcdfdbecf5723067df3e24e4f6f0?d=identicon&s=25 Adam Block (adamblock)
on 2007-01-20 20:22
Hi.

Assume you have an array of person names. I want to generate results in
my view that look like this:

Abby is your friend

or

Abby, Bob, and Carol are your friends.

So I'd like to say:

    <%= friends.to_sentence %>
    <%= pluralize(friends.count, "is") %>
    your <%= pluralize(friends.count, "friend") %>

But because pluralize puts in the number, I get:

Abby 1 is your 1 friend

or

Abby, Bob, and Carol 3 are your 3 friends.

Is there a way around this? The API only shows the one form for
pluralize.

Thanks!

/afb
70ca58d0e0e0eabbdb74d177417d09d7?d=identicon&s=25 augustlilleaas@gmail.com (Guest)
on 2007-01-20 20:48
(Received via mailing list)
Perhaps this helps:

http://api.rubyonrails.org/classes/Inflector.html#M001076

Kind of strange, though. The Sting::Inflections simply do a
Inflection.pluralize(word), and that method seems to return the word
only (the method I'm talking about is the one in the link above).
Weird.

On Jan 20, 8:22 pm, Adam Block <rails-mailing-l...@andreas-s.net>
315035a47dcb93f4427b8a1e18da4ff7?d=identicon&s=25 Jim Lindley (Guest)
on 2007-01-20 21:26
(Received via mailing list)
You'll need to use to_sentence to make a custom helper, in
application_helper.rb:

def friends_list_to_sentence(friends)
  if friends.to_a.count == 1
    friends + " is your friend."
  else
    friends.to_sentence + " are your friends."
  end
end

That's quick and dirty and I didn't test it.

On 1/20/07, augustlilleaas@gmail.com <augustlilleaas@gmail.com> wrote:
> On Jan 20, 8:22 pm, Adam Block <rails-mailing-l...@andreas-s.net>
> > Abby, Bob, and Carol are your friends.
> >
> >
> > --
> > Posted viahttp://www.ruby-forum.com/.
>
>
> >
>


--
Jim
--------------------
Jim Lindley
jimlindley@gmail.com
This topic is locked and can not be replied to.