RUBY |找到一种方法,在同一个单词上找到一个例外,并大写

RUBY |找到一种方法,在同一个单词上找到一个例外,并大写,ruby,Ruby,您好,我向您展示了我的一段代码,渲染 “桂河大桥” 虽然我想 “桂河大桥” 因此,我想找到一种方法来查找同一单词的异常情况。您的代码,但始终将第一个字母大写,使用带有索引的每个字母来获取数组中的位置: def titleize(string) nocaps = ["the","and"] puts string.split(" ").map { |word| nocaps.include?(word) ? word : word.ca

您好,我向您展示了我的一段代码,渲染 “桂河大桥” 虽然我想 “桂河大桥”
因此,我想找到一种方法来查找同一单词的异常情况。

您的代码,但始终将第一个字母大写,使用
带有索引的每个字母来获取数组中的位置:

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"
一种方法如下

def titleize(string)
  nocaps = ["the","and"]
  string.split(" ").each_with_index.map { |word, i| i.positive? && nocaps.include?(word) ? word : word.capitalize }.join(" ")
end

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

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

正则表达式的内容是,“匹配一个或多个Unicode字母(
\p{L}+
)(贪婪地)前面加一个空格”

这种方法的一种变体是将
little_words
中未包含的所有单词大写,然后将结果字符串的第一个字符大写:

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

如果
little\u words
包含许多单词,则可以通过首先将该数组转换为一个集合
little\u words\u set
需要'set';little\u words\u set=little\u words.to\u set
),然后在出现
little\u words
的任何地方替换
来加快方法的速度

这可以通过从@Todd的答案中选择一页来改进:将
downcase
替换为
大写
,省去了
点击
子句的需要


请注意,这是一个在出版业中使用的术语。

虽然其他答案都是针对您的原始帖子,但我想我会提供另一个使用正则表达式的替代方法

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
此正则表达式将选择第一个字母
\A.
nocaps
数组中未包含的任何其他单词
,然后用大写版本替换每个选定单词

在这种情况下,生成的正则表达式是
/\A.|\b(?)(?-mix:The | and))\b\w+/

在每个单词大写之前将字符串大写 有很多方法可以做到这一点,但其中一种方法是在决定是否将单个单词大写之前将整个字符串对象大写。例如:

def titleize(str) 
  nocaps = ["the","and"]
  str.gsub(/\A.|\b(!?#{Regexp.union(nocaps)})\b\w+/,&:capitalize) 
end 
titleize("the bridge over the river and through the woods kwai")
#=> "The Bridge Over the River and Through the Woods Kwai"

实际上,除了第一个字母外,这将使所有单词都大写,然后通过编程将每个不在stopwords中的单词大写。因此,它依赖于一些隐式行为,但对发布的示例效果良好。

@jad examples:“and”=>“and”或“冬天来了”=>“冬天来了”我认为Rails标签不合适,因为这是一个纯Ruby问题。@pharaoh2000:不会
标题化。大写
来做你想做的事吗?如果不是,我担心你的问题不是很清楚。@user1934428不完全清楚
大写
:“返回str的副本,第一个字符转换为大写,其余字符转换为小写。”@engineersmnky:非常感谢!我没有意识到这一点。谢谢,这对我有用!非常感谢你。
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"
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
def titleize(str) 
  nocaps = ["the","and"]
  str.gsub(/\A.|\b(!?#{Regexp.union(nocaps)})\b\w+/,&:capitalize) 
end 
titleize("the bridge over the river and through the woods kwai")
#=> "The Bridge Over the River and Through the Woods Kwai"
def titleize str 
  stopwords   = %w[the and]
  title_words = str.capitalize.split.map! do |word| 
    stopwords.include?(word) ? word : word.capitalize
  end.join ?\s
end

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

p titleize("the bridge over the river kwai and the amazon")
#=> "The Bridge Over the River Kwai and the Amazon"