Ruby: Titleize: How to ignore smaller words like 'and', '', 'or, etc.

def titleize(string) string.split(" ").map {|word| word.capitalize}.join(" ") end 

This is the name of each word, but how can I capture certain words that I do not want to use?

i.e.) Jack and Jill

AND DO NOT USE Regex.

UPDATE:

I had trouble creating this code: I got it to print an array of words with all the caps, but not without the list below.

 words_no_cap = ["and", "or", "the", "over", "to", "the", "a", "but"] def titleize(string) cap_word = string.split(" ").map {|word| word.capitalize} cap_word.include?(words_no_cap) end 
+7
source share
9 answers

You probably want to create an extension for the existing titleize function that Rails provides.

To do this, simply include the following file in the initializer and presto! Throw exceptions on the fly, or perhaps modify my example to add default values ​​to the initializer.

I understand that you did not want to use Regex, but indeed, the rails function uses Regex so that you can synchronize it.

Put this file in Rails.root/lib/string_extension.rb and load it into the initializer; or just throw it in the initializer itself.

UPDATE: changed REGEX on this, thanks to @svoop's suggestion for adding the border of the final word.

 # encoding: utf-8 class String def titleize(options = {}) exclusions = options[:exclude] return ActiveSupport::Inflector.titleize(self) unless exclusions.present? self.underscore.humanize.gsub(/\b(?<![''`])(?!(#{exclusions.join('|')})\b)[az]/) { $&.capitalize } end end 
+6
source

Here is my little code. You can refract it in several lines.

 def titleize(str) str.capitalize! # capitalize the first word in case it is part of the no words array words_no_cap = ["and", "or", "the", "over", "to", "the", "a", "but"] phrase = str.split(" ").map {|word| if words_no_cap.include?(word) word else word.capitalize end }.join(" ") # I replaced the "end" in "end.join(" ") with "}" because it wasn't working in Ruby 2.1.1 phrase # returns the phrase with all the excluded words end 
+3
source

If you move this in config / initializers to a new file (you can call it something like string.rb), you can call your custom functions for any line. Make sure you reboot and then you can work below, for example ex) "anystring" .uncapitalize_puncs

This is easier than bothering with trying to change the default derivation code. So now you can just call @ something.title.titleize.uncapitalize_puncs

 class String def uncapitalize_puncs puncs = ["and", "the", "to", "of", "by", "from", "or"] array = self.split(" ") array.map! do |x| if puncs.include? x.downcase x.downcase else x end end return array.join(" ") end end 
+3
source

If you want to not capitalize and and, simply follow these steps:

 def titleize(string) nocaps = "and" string.split(" ").map { |word| nocaps.include?(word) ? word : word.capitalize }.join(" ") end 
+3
source

@Codenamev's answer doesn't do the job:

 EXCLUSIONS = %w(a the and or to) "and the answer is all good".titleize(exclude: EXCLUSIONS) # => "And the Answer Is all Good" ^^^ 

Exceptions must match the final word boundaries. Here's an improved version:

 # encoding: utf-8 class String def titleize(options = {}) exclusions = options[:exclude] return ActiveSupport::Inflector.titleize(self) unless exclusions.present? self.underscore.humanize.gsub(/\b([''`]?(?!(#{exclusions.join('|')})\b)[az])/) { $&.capitalize } end end "and the answer is all good".titleize(exclude: EXCLUSIONS) # => "And the Answer Is All Good" ^^^ 
+1
source

metodo capize para para títulos

 def capitalizar_titulo(string) words_not_to_capitalize = ["a","e","i","o","u","ao","aos", "as", "nos","nós","no","na","mas", "mes", "da", "de", "di", "do", "das", "dos"] s = string.split.each{|str| str.capitalize! unless words_not_to_capitalize.include? (str.downcase) } s[0].capitalize + " " + s[1..-1].join(" ") end 
+1
source

It's pretty simple, just add a condition when you call captalize :

 $nocaps = ['Jack', 'Jill'] def titleize(string) string.split(" ").map {|word| word.capitalize unless $nocaps.include?(word)}.join(" ") end 

In this example, global variables are far-fetched, probably it will be an instance variable in your real application.

0
source

Some headlines have extreme cases (pun intended) that you may need.

For example, small words at the beginning of a title or after punctuation often need to be capitalized (for example, “The Chronicles of Narnia: The Lion, the Witch and the Wardrobe,” which has both).

It may also be necessary / necessary to force small words into lower case, so that the input signal like "Jack and Jill" is transmitted to "Jack and Jill".

Sometimes you may also need to determine when a word (usually brand names) should retain unusual capitalization, for example. "iPod" or acronyms, for example. "NATO" or domain names, "example.com".

To properly handle such cases, the titleize gem is your friend or should at least serve as the basis for a complete solution.

0
source
 titleize("the matrix or titanic") def titleize(string) no_cap = ["and", "or", "the", "over", "to", "the", "a", "but"] string.split(" ").map { |word| no_cap.include?(word) ? word : word.capitalize }.join(" ") end 

result:

 "the Matrix or Titanic" 
0
source

All Articles