Forked from sferik/number_with_indefinite_article.rb
Created
January 27, 2012 01:49
-
-
Save trafnar/1686420 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'linguistics' | |
# Helper function to pick the correct indefinite article for a number | |
# e.g. number_with_indefinite_article(10) #=> "a 10" | |
# e.g. number_with_indefinite_article(80, "$") #=> "an $80" | |
def number_with_indefinite_article(number, prefix=nil) | |
if [11,18].include?(number) | |
article = "an" | |
elsif number.to_s[0] == "8" | |
article = "an" | |
else | |
word = Linguistics::EN.numwords(number) | |
if ["eleven", "eighteen"].include?(word.split(" ")[0]) | |
article = "an" | |
else number.to_s[0] == "1" | |
article = "a" | |
end | |
end | |
[article, " ", prefix, number].compact.join | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I believe only 8, 11, 18 and any subsequent numbers beginning with the number 8, or the words "eleven" or "eighteen" should use "an", so my version just looks for those special cases. the number 1 starts with a "w" sound so it should use "a" rather than "an", which fails the vowel test.