0

私はこれに本当に近づいているように感じますが、なぜ機能しないのかわかり.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
4

3 に答える 3

2

.mapの代わりに使用.each:

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
于 2013-09-02T07:33:53.017 に答える