Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.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 将字符串转换为标题大小写_Ruby_Rspec - Fatal编程技术网

Ruby 将字符串转换为标题大小写

Ruby 将字符串转换为标题大小写,ruby,rspec,Ruby,Rspec,我觉得我和这个很接近,但我不明白为什么。join不起作用 这是我写的代码: class String def title_case title = self.split title.each do |word| unless (word.include?("of")) || (word.include?("the")) && (title.first != "the") word.capitalize! end

我觉得我和这个很接近,但我不明白为什么
。join
不起作用

这是我写的代码:

class String
  def title_case
    title = self.split
    title.each do |word|
      unless (word.include?("of")) || (word.include?("the")) && (title.first != "the")
        word.capitalize!
      end
    title.join(" ")
    end
  end
end
这是RSPEC:

describe "String" do
  describe "Title case" do
    it "capitalizes the first letter of each word" do
      "the great gatsby".title_case.should eq("The Great Gatsby")
    end
    it "works for words with mixed cases" do
      "liTTle reD Riding hOOD".title_case.should eq("Little Red Riding Hood")
    end
    it "ignores articles" do
      "The lord of the rings".title_case.should eq("The Lord of the Rings")
    end
  end
end

如果您正确格式化了代码,就会发现您将
#join
调用放错了位置。它需要在每个循环之外

def title_case
  title = self.split
  title.each do |word|
    unless (word.include?("of")) || (word.include?("the")) && (title.first != "the")
      word.capitalize!
    end
  end
  title.join(" ")
end

但是使用
map
和无损
大写
(如@xdazz的答案中所示)将更为惯用。

如果您正确格式化代码,您会发现您将
\join
调用放错了位置。它需要在每个循环之外

def title_case
  title = self.split
  title.each do |word|
    unless (word.include?("of")) || (word.include?("the")) && (title.first != "the")
      word.capitalize!
    end
  end
  title.join(" ")
end

但是使用
map
和无损
大写
(如@xdazz的回答)将更为惯用。

使用
.map
而不是
。每个

class String
  def title_case
    title = self.split
    title.map do |word|
      unless (word.include?("of")) || (word.include?("the")) && (title.first != "the")
        word.capitalize
      end
    end.join(" ")
  end
end

使用
.map
而不是
。每个

class String
  def title_case
    title = self.split
    title.map do |word|
      unless (word.include?("of")) || (word.include?("the")) && (title.first != "the")
        word.capitalize
      end
    end.join(" ")
  end
end
稍微(细微的)重新缩进显示出您的问题:

class String
  def title_case
    title = self.split
    title.each do |word|
      unless (word.include?("of")) || (word.include?("the")) && (title.first != "the")
        word.capitalize!
      end
      title.join(" ")
    end # End of each
  end # End of def
end
您正在将调用的值返回给每个
。解决方法是将
title.join(“”)
向下移动一行,在每个
的结尾之后,在方法定义的结尾之前。

稍微(细微的)重新缩进显示您的问题:

class String
  def title_case
    title = self.split
    title.each do |word|
      unless (word.include?("of")) || (word.include?("the")) && (title.first != "the")
        word.capitalize!
      end
      title.join(" ")
    end # End of each
  end # End of def
end
您正在将调用的值返回给每个
。解决方法是将
title.join(“”
向下移动一行,在每个
的结尾之后,在方法定义的结尾之前