Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Singularizing singular words added with #irregular does not work #8605

Closed
jackrg opened this Issue Dec 25, 2012 · 4 comments

Comments

Projects
None yet
4 participants

jackrg commented Dec 25, 2012

In my initializer, I write:

ActiveSupport::Inflector.inflections { |inflect| inflect.irregular("is", "are") }

Then the following occurs: "is".pluralize => "are", "are".singularize => "is", "are".pluralize => "are",
but "is".singularize => "i".

I traced this down to the following code in active_support / inflector / methods.rb:

def irregular(singular, plural)
  ...
  if singular[0,1].upcase == plural[0,1].upcase
    ...
  else
    plural(Regexp.new("#{singular[0,1].upcase}(?i)#{singular[1..-1]}$"), plural[0,1].upcase + plural[1..-1])
    plural(Regexp.new("#{singular[0,1].downcase}(?i)#{singular[1..-1]}$"), plural[0,1].downcase + plural[1..-1])
    plural(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), plural[0,1].upcase + plural[1..-1])
    plural(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), plural[0,1].downcase + plural[1..-1])
    singular(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), singular[0,1].upcase + singular[1..-1])
    singular(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), singular[0,1].downcase + singular[1..-1])
  end
end

Note that the code does not include the following lines:

singular(Regexp.new("#{singular[0,1].upcase}(?i)#{singular[1..-1]}$"), singular[0,1].upcase + singular[1..-1])
singular(Regexp.new("#{singular[0,1].downcase}(?i)#{singular[1..-1]}$"), singular[0,1].downcase + singular[1..-1])

which would ensure that "is".singular => "is". It seems like this is an oversight.

Member

steveklabnik commented Dec 25, 2012

It seems like this is an oversight.

I would agree that it is.

Contributor

al2o3cr commented Dec 26, 2012

This happens more than you'd expect - singularize does not always have a well-defined fixed-point, so you probably shouldn't singularize things you know are singular already. An example:

irb: s = "pass"
===> "pass"
irb: s.pluralize
===> "passes"
irb: s.pluralize.singularize
===> "pass"
irb: s.pluralize.singularize.singularize
===> "pas"

jackrg commented Dec 26, 2012

I can't disagree with you in general. However, I would consider it a best practice to protect against such things when you can. #irregular is one of those occasions when you could.

Sent from my iPhone; please excuse the brevity and any typos.

On Dec 25, 2012, at 5:53 PM, Matt Jones notifications@github.com wrote:

This happens more than you'd expect - singularize does not always have a well-defined fixed-point, so you probably shouldn't singularize things you know are singular already. An example:

irb: s = "pass"
===> "pass"
irb: s.pluralize
===> "passes"
irb: s.pluralize.singularize
===> "pass"
irb: s.pluralize.singularize.singularize
===> "pas"

Reply to this email directly or view it on GitHub.

Closed by #8671

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment