Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/englishext.rb
Instance Method Summary collapse
-
#camel_case_to_english ⇒ Object
Turns a camel-case string (“camel_case_to_english”) to plain English (“camel case to english”).
-
#english_to_camel_case ⇒ Object
Turns an English language string into camel case.
-
#plural ⇒ Object
Given a singular noun, returns the plural form.
-
#proper_noun ⇒ Object
Returns the proper noun form of a string by capitalizing most of the words.
- #singular ⇒ Object
-
#starts_with_vowel_sound ⇒ Object
Does this word start with a vowel sound? “User” and “usury” don’t, but “ugly” does.
-
#template(name, number = 1) ⇒ Object
Given a format for a template sentence, generates the sentence while accounting for details such as pluralization and whether to use “a” or “an”.
Instance Method Details
#camel_case_to_english ⇒ Object
Turns a camel-case string (“camel_case_to_english”) to plain English (“camel case to english”). Each word is decapitalized.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/englishext.rb', line 9 def camel_case_to_english words = [] str = self.clone nextCapIndex =(str =~ /[A-Z]/) while nextCapIndex != nil words << $` if $`.size > 0 str = $& + $' str[0] = str[0..0].downcase nextCapIndex =(str =~ /[A-Z]/) end words << str words.join ' ' end |
#english_to_camel_case ⇒ Object
Turns an English language string into camel case.
24 25 26 27 28 29 30 31 |
# File 'lib/englishext.rb', line 24 def english_to_camel_case cc = "" split.each { |word| word = word.capitalize unless cc == '' cc = cc += word } cc end |
#plural ⇒ Object
Given a singular noun, returns the plural form.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/englishext.rb', line 34 def plural consonantYPattern = Regexp.new("([^aeiou])y$", Regexp::IGNORECASE) if self =~ consonantYPattern self.gsub consonantYPattern, '\1ies' elsif self =~ /^(.*)xis$/ $1 + 'xes' elsif self =~ /[xs]$/ self + "es" elsif self =~ /(.*)tum$/ $1 + 'ta' else self + "s" end end |
#proper_noun ⇒ Object
Returns the proper noun form of a string by capitalizing most of the words.
Examples:
"bosnia and herzegovina".proper_noun -> "Bosnia and Herzegovina"
"macedonia, the former yugoslav republic of".proper_noun ->
"Macedonia, the Former Yugoslav Republic of"
"virgin islands, u.s.".proper_noun -> "Virgin Islands, U.S."
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/englishext.rb', line 57 def proper_noun proper_noun = "" quote_seen = false str = self.clone while(matchIndex = str =~ /[\. \-\']/) if matchIndex == 0 proper_noun += $& str = str[matchIndex+1..str.length] else word = str[0..matchIndex-1] unless [ 'and', 'the', 'of' ].include?(word) or (quote_seen and word == 's') word = word.capitalize end quote_seen = $& == "'" proper_noun += word + $& str = str[matchIndex+1..str.length] end end word = str unless [ 'and', 'the', 'of' ].include?(word) or (quote_seen and word == 's') word = word.capitalize end proper_noun += word proper_noun end |
#singular ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/englishext.rb', line 85 def singular if self =~ /(.*)ies/ $1 + 'y' elsif self =~ /(.*s)es/ $1 else self =~ /(.*)s/ $1 end end |
#starts_with_vowel_sound ⇒ Object
Does this word start with a vowel sound? “User” and “usury” don’t, but “ugly” does.
98 99 100 101 102 |
# File 'lib/englishext.rb', line 98 def starts_with_vowel_sound uSomethingUMatch = self =~ /^u[^aeiuo][aeiou]/ # 'user' and 'usury' don't start with a vowel sound self =~ /^[aeiou]/ && !uSomethingUMatch end |
#template(name, number = 1) ⇒ Object
Given a format for a template sentence, generates the sentence while accounting for details such as pluralization and whether to use “a” or “an”.
Format codes are:
* %num: Number
* %is: Transitive verb. This will be turned into "is" or "are",
depending on <tt>number</tt>.
* %nam: Name. This will be rendered as either singular or
plural, depending on <tt>number</tt>.
* %a: Indefinite article. This will be turned into "a" or "an",
depending on <tt>name</tt>.
- name
-
The name of the object being described.
- number
-
The number of the objects being describes.
Examples:
"There %is currently %num %nam".template( "product category", 0 ) ->
"There are currently 0 product categories"
"There %is currently %num %nam".template( "product category", 1 ) ->
"There is currently 1 product category"
"Add %a %nam".template( "invoice" ) -> "Add an invoice"
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/englishext.rb', line 125 def template( name, number = 1) sentence = clone sentence.gsub!( /%num/, number.to_s ) isVerb = number == 1 ? "is" : "are" sentence.gsub!( /%is/, isVerb ) name = name.plural if number != 1 sentence.gsub!( /%nam/, name ) article = name.starts_with_vowel_sound ? 'an' : 'a' sentence.gsub!( /%a/, article ) sentence end |