Conditional pluralize without the number


#1

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


#2

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 B. removed_email_address@domain.invalid


#3

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, removed_email_address@domain.invalid removed_email_address@domain.invalid wrote:

On Jan 20, 8:22 pm, Adam B. removed_email_address@domain.invalid

Abby, Bob, and Carol are your friends.


Posted viahttp://www.ruby-forum.com/.


Jim

Jim L.
removed_email_address@domain.invalid