RUBY循环和if语句没有正确执行

时间:2015-08-07 20:00:57

标签: ruby loops

我是Ruby编程员,完成与构建LOOP相关的家庭作业。 RSPEC问题的标准如下所示。我尝试了很多方法。

我已经能够通过前两个条件,但我完全坚持如何匹配和转换英文文章(a,an,the)。我似乎没有正确地循环并在方法中正确使用if then构造。

您能否提出我可以解决此问题的建议,或者是否存在我缺少的简单语法问题?

describe "Title" do   describe "fix" do
  it "capitalizes the first letter of each word" do       
    expect( Title.new("the great gatsby").fix ).to eq("The Great Gatsby")
  end     

  it "works for words with mixed cases" do       
    expect( Title.new("liTTle reD Riding hOOD").fix ).to eq("Little Red Riding Hood")
  end

  it "downcases articles" do
    expect( Title.new("The lord of the rings").fix ).to eq("The Lord of the Rings")
    expect( Title.new("The sword And The stone").fix ).to eq("The Sword and the Stone")
    expect( Title.new("the portrait of a lady").fix ).to eq("The Portrait of a Lady")
  end

  it "works for strings with all uppercase characters" do
    expect( Title.new("THE SWORD AND THE STONE").fix ).to eq("The Sword and the Stone")
  end
end

这是我到目前为止提出的代码:

class Title 
  def initialize(book)     
    @book=book  
  end   

  def fix   
    #create an array of articles    
    art = ['a','an','the']       
    #reformat any string to all lower case to reset to a common format
    @book.downcase.split(' ').map {|w| w.capitalize }.join(' ')      
    #resplit the oll lower case string into an array
    arr = @book.split(' ')      
    #for each element of the array check if in art array if tru
    arr.each_with_index do |element, index|        
      if art.include?(element) then element.downcase
      elsif index == 1 then element.upcase 
      else element.upcase  
      end
    end
    arr.join(' ')       
  end   
end

以下是我收到的错误消息:

•   RSpec::Expectations::ExpectationNotMetError
 expected: "The Great Gatsby"      got: ["the", "great", "gatsby"]  (compared using ==)  
exercise_spec.rb:6:in `block (3 levels) in <top (required)>'
•    Title fix works for words with mixed cases
RSpec::Expectations::ExpectationNotMetError
 expected: "Little Red Riding Hood"      got: ["liTTle", "reD", "Riding", "hOOD"]  (compared using ==)  
exercise_spec.rb:9:in `block (3 levels) in <top (required)>'
•    Title fix downcases articles
RSpec::Expectations::ExpectationNotMetError
 expected: "The Lord of the Rings"      got: ["The", "lord", "of", "the", "rings"]  (compared using ==)  
exercise_spec.rb:12:in `block (3 levels) in <top (required)>'
•    Title fix works for strings with all uppercase characters
RSpec::Expectations::ExpectationNotMetError
 expected: "The Sword and the Stone"      got: ["THE", "SWORD", "AND", "THE", "STONE"]  (compared using ==)  
exercise_spec.rb:17:in `block (3 levels) in <top (required)>'

2 个答案:

答案 0 :(得分:2)

这里有几个问题。

  1. 在很多地方,您没有分配变量。例如,element.upcase,不会将element.upcase分配给element。为此,您需要说element = element.upcaseelement.upcase!
  2. 说到upcase .. upcase是为了将整个单词大写,您想使用capitalize(或capitalize!
  3. Ruby数组是零索引的,因此在检查您要使用的第一个单词时索引== 0
  4. 你的if / else的顺序是不对的。现在,对于第一个单词,你检查它是否是一篇文章(如果它不是你没有大写)。它永远不会检查它是否是之后的第一个单词。
  5. 你错过了&#39;和&#39;在文章数组

答案 1 :(得分:0)

循环正确执行,只是我认为你写的并不是你想要的。我们有一本书:

@book
# => "a The b"

您应该使用简单的方法重新格式化为小写:

arr = @book.downcase.split(/\s+/)
# => ['a', 'the', 'b']

要使用#map方法生成新修改的数组:

arr.map.with_index do |element, index|        
  art.include?(element) && element || element.capitalize
end.join(' ')

# => "A the B"