Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 标题中大写大词的Titileize方法_Ruby - Fatal编程技术网

Ruby 标题中大写大词的Titileize方法

Ruby 标题中大写大词的Titileize方法,ruby,Ruby,我正在研究一种方法,将传入的句子中每个单词的第一个字母大写,除非是其中一个小词。(无论发生什么情况,句子中的第一个单词总是大写) 我有以下资料: def titleize(t) little = ["over", "the", "and"] q = t.split(" ") u = [] q.each do |i| p = i.split("") p[0] = p[0].upcase r = p.join("")

我正在研究一种方法,将传入的句子中每个单词的第一个字母大写,除非是其中一个小词。(无论发生什么情况,句子中的第一个单词总是大写)

我有以下资料:

def titleize(t)
little = ["over", "the", "and"]
q = t.split(" ")
u = []

    q.each do |i|
        p = i.split("")
        p[0] = p[0].upcase
        r = p.join("")
            if i == q[0]
                u.push(r)
            elsif i == little[0] || i == little[1] || i == little[2]
                u.push(i)
            else
                u.push(r)
            end
        end
    s = u.join(" ")
    return s

end
当我通过测试时,我得到:

     Failure/Error: titleize("the bridge over the river kwai").should == "The Br
     idge over the River Kwai"
       expected: "The Bridge over the River Kwai"
       got: "The Bridge over The River Kwai" (using ==)

为什么句子中的第二个“the”变为大写

Edit:这是我最初建议的另一个解决方案,代码如下:

class String
  def titleize(exclusions)
    words = self.split.map do |word|
      exclusions.include?(word) ? word : word.capitalize
    end
    words.first.capitalize!
    words.join(" ")
  end
end

string = "the bridge over the river kwai"
exclusions = %w{ over the and }
p string.titleize(exclusions)
  #=> "The Bridge over the River Kwai"
原始答复: 将整个字符串中的每个单词大写,然后在适当的时候使用gsub将这些单词替换为小写版本,怎么样

string = "the bridge over the river kwai"
exclusions = %w{ over the and }
p string.split.map(&:capitalize).join(" ").gsub(/(?<!^)(#{exclusions.join("|")})/i) {|word| word.downcase}
  #=> "The Bridge over the River Kwai"
string=“桂河大桥”
排除项=%w{在和上}
p string.split.map(&:capitalize).join(“”.gsub(/(?“桂河大桥”)

因为
q[0]
“the”
,所以当
i
“the”
时,它满足以下条件:

i == q[0]
更好的方法是:

Little = %w[over the and]
def titleize s
  s.gsub(/\w+/)
  .with_index{|w, i| i.zero? || Little.include?(w).! ? w.capitalize : w}
end

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

上述的一种变体:

def titleize(s)
  s.sub( /\w+/) { |w| w.capitalize }
   .gsub(/\w+/) { |w| Little.include?(w) ? w : w.capitalize }
end
试试看:

s =<<THE_END
to protect his new shoes from the rain, a man stood atop a stack of
newspapers in Times Square.  When the newsstand's proprietor asked
if he realized what he was standing on, he replied, "of course,
these are the times that dry men's soles".
THE_END

Little = ["the", "and", "a", "on", "to", "from", "in"]

titleize(s)
  #=> "To Protect His New Shoes from the Rain, a Man Stood Atop a Stack Of \n
  #    Newspapers in Times Square.  When the Newsstand'S Proprietor Asked\n
  #    If He Realized What He Was Standing on, He Replied, \"Of Course,\n
  #    These\nAre the Times That Dry Men'S Soles\".\n"

s=因为你有
u.push(r)
在那种情况下。我说的是第二个词,而不是我现在理解的第一个词,嗯,那么我如何解决这个问题呢,我需要大写第一个词,不管是什么,也不知道如何解决。这是另一个问题。我的回答是否回答了你的问题?好的,我给出了一种方法。它没有回答问题。注意
self.split(" ")
可以缩短为
split
。另外,我认为
str
不是数组名称的好选择。感谢@CarySwoveland的建议。我总是忘记在没有任何参数的情况下使用split。我会更新我的答案。你也不需要
self
,因为它是默认的接收器。我知道有些人喜欢这样做出于风格上的原因,应该包括
self
,但我的阵营认为,不应该包括不必要的元素,以免读者认为出于某种原因需要这些元素。当然,也有一些情况需要
self
,例如
self.class
self.a_访问者
self[2]边注释,考虑使用描述性变量名,如“代码>语句,<代码>文字>代码>代码>字符<代码>,而不是<代码> t>代码>,代码> q>代码>,代码> p>代码>。可读性很强。您选择了“SAWA”的答案,但未被投票赞成。您没有义务对任何答案进行投票,但我作为您提到这一点。一个新成员,可能没有意识到你可以投票给你选择的答案。