红宝石 |找到一种方法在同一个单词上找到一个例外来大写 [英] RUBY | Find a way to find an exception on the same word to capitalize

查看:46
本文介绍了红宝石 |找到一种方法在同一个单词上找到一个例外来大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def titleize(string)

nocaps = ["the","and"]
puts string.split(" ").map { |word| nocaps.include?(word) ? word : word.capitalize }.join(" ")

end

titleize("the bridge over the river kwai")
------------------------------------------------------------------------------------------
######Render => "the Bridge Over the River Kwai"

######Expected rendering => "The Bridge Over the River Kwai"

你好,我向你展示了我的一段代码,渲染给出了《桂河大桥》虽然我想《桂河大桥》所以想找个方法,在同一个词上找到异常.

Hello i present you my piece of code, the rendering gives "the Bridge Over the River Kwai" While I would like "The Bridge Over the River Kwai" So I would like to find a way to find an exception on the same word.

推荐答案

一种方式如下.

def titleize(str, little_words)
  str.downcase.split.map.with_index do |word,i|
    little_words.include?(word) && i > 0 ? word : word.capitalize
  end.join(" ")
end

str = "the briDge over   The river kwai"    

titleize(str, ["the", "and"])
  #=> "The Bridge Over the River Kwai"

这是直接对字符串进行操作的第二种方法(而不是将其转换为数组或单词,对这些单词执行替换然后加入结果数组):

Here's a second way that operates on the string directly (rather than converting it to an array or words, perform substitutions for those words and then joining the resulting array):

def titleize(str, little_words)
  str.downcase.gsub(/(?<= )\p{L}+/) do |s|
    little_words.include?(s) ? s : s.capitalize
  end
end

titleize(str, ["the", "and"])
  #=> "The Bridge Over   the River Kwai"

请注意,此方法会在 str 中保留单词之间的额外空格.

Notice that this method preserves extra spaces between words in str.

正则表达式读取,匹配一个或多个 Unicode 字母 (\p{L}+)(贪婪地)".

The regular expression reads, "match one or more Unicode letters (\p{L}+) (greedily) preceded by a space".

一个变体是将所有不在 little_words 中的单词大写,然后将结果字符串的第一个字符大写:

A variant of this is to capitalize all words not in little_words and then capitalize the first character of the resulting string:

def titleize(str, little_words)
  str.downcase.gsub(/\p{L}+/) do |s|
    little_words.include?(s) ? s : s.capitalize
  end.tap { |s| s[0] = s[0].upcase }
end

请参阅Object#tap.

如果 little_words 包含许多单词,则可以通过首先将该数组转换为集合 little_words_set(require 'set'; little_words_set = little_words.to_set) 然后替换 little_words_set 现在出现 little_words 的地方.

If little_words contains many words the methods could be sped up by first converting that array to a set little_words_set (require 'set'; little_words_set = little_words.to_set) and then substituting little_words_set wherever little_words now appears.

这可以通过从@Todd 的回答中获取一个页面来改进:将 downcase 替换为 capitalize,从而无需 tap 子句.

This can be improved upon by taking a page from @Todd's answer: replace downcase with capitalize, obviating the need for the tap clause.

注意little词是出版业中使用的一个术语.

Note that little words is a term used in the publishing industry.

这篇关于红宝石 |找到一种方法在同一个单词上找到一个例外来大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆