Forum: Ruby on Rails pluralization

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.
69b2ef4bce76b5b27c94e898976dc6d8?d=identicon&s=25 winescout (Guest)
on 2005-12-07 08:40
(Received via mailing list)
I got curious about Active Record playing tricks with pluralization, and
found a few places in my rig where it might be useful.  If you want to
play
with it yourself, gem install localization, and you'll get it.  There's
some
other cool stuff in there too!

Anyhow, I'm a bit stuck, or braindead. (actually both tonight) and could
use
some fresh ideas.

What I want to do is test a string to see if it is a plural, and if so
branch the code and do something else with it.  Localization has a
method to
convert from singular to plural, and back again, but as far as I can
tell,
no way just to test plurality.  Can you think of a way to do this test?
Here are some tests that should make my meaning clear.

require File.dirname(__FILE__) + '/../test_helper'
require 'linguistics'
Linguistics::use( :en)

class LinguisticsTest < Test::Unit::TestCase
  def test_conversion
    assert_equal("mice","mouse".en.plural)
  end

  def test_converting_plural_word
    assert_equal("mice","mice".en.plural, "mice not recognized as
plural")
  end

  def test_converting_plural_word_2
    assert_equal("cats", "cats".en.plural, "cats not recognized as
plural")
  end

  #if there were a truth function, this would be it
  def test_plural_truth
    assert "cats".en.plural?
  end
end

============== RESULTS ==================

Loaded suite linguistics_test
Started
.FFE
Finished in 0.112234 seconds.

  1) Failure:
test_converting_plural_word(LinguisticsTest) [linguistics_test.rb:12]:
mice not recognized as plural.
<"mice"> expected but was
<"mices">.

  2) Failure:
test_converting_plural_word_2(LinguisticsTest) [linguistics_test.rb:16]:
cats not recognized as plural.
<"cats"> expected but was
<"cat">.

  3) Error:
test_plural_truth(LinguisticsTest):
NoMethodError: undefined method `plural?' for <English languageProxy for
String object "cats">:#<Class:0xb7627b44>
    /usr/lib/ruby/gems/1.8/gems/Linguistics-1.0.3/lib/linguistics.rb:90:in
`method_missing'
    linguistics_test.rb:21:in `test_plural_truth'

4 tests, 3 assertions, 2 failures, 1 errors
Ce60c4f78a63b0695e4dafc4bd7964f7?d=identicon&s=25 vanek (Guest)
on 2005-12-08 18:29
(Received via mailing list)
there is a plurization paper and Perl program at
	http://www.csse.monash.edu.au/~damian/papers/
(look for 'English Plurals' on the page).
You may be able to steal\h\h\h\h\hrecycle the algorithm
and make a ruby port.


matthew clark wrote:

> method to convert from singular to plural, and back again, but as far as
> I can tell, no way just to test plurality.

[snip]
5b9fe87ec1faa67a4599782930f45ec9?d=identicon&s=25 sstephenson (Guest)
on 2005-12-08 19:59
(Received via mailing list)
Hi,

On 12/7/05, matthew clark <winescout@gmail.com> wrote:
>  What I want to do is test a string to see if it is a plural, and if so
> branch the code and do something else with it.

Not sure about the localization gem, but it's pretty trivial using
Active Support's inflector:

class String
  def plural?
    self == self.pluralize
  end
end

'cow'.plural?  # => false
'cows'.plural? # => true

--
sam
This topic is locked and can not be replied to.