Ruby条件测试

Ruby条件测试,ruby,testing,rspec,Ruby,Testing,Rspec,我无法使我的代码通过此测试: it "translates two words" do s = translate("eat pie") s.should == "eatay iepay" end 我看不出我的逻辑有什么缺陷,尽管这可能是非常残酷的,而且可能有一种更简单的方式通过测试: def translate(string) string_array = string.split string_length = string_array.size

我无法使我的代码通过此测试:

it "translates two words" do
    s = translate("eat pie")
    s.should == "eatay iepay"
  end
我看不出我的逻辑有什么缺陷,尽管这可能是非常残酷的,而且可能有一种更简单的方式通过测试:

def translate(string)
    string_array = string.split
    string_length = string_array.size
    i=0

    while i < string_length
        word = string_array[i]
        if word[0] == ("a" || "e" || "i" || "o" || "u")
            word = word + "ay"
            string_array[i] = word

        elsif word[0] != ( "a" || "e" || "i" || "o" || "u" ) && word[1] != ( "a" || "e" || "i" || "o" || "u" )
            word_length = word.length-1
            word = word[2..word_length]+word[0]+word[1]+"ay"
            string_array[i] = word

        elsif word[0] != ( "a" || "e" || "i" || "o" || "u" )
            word_length = word.length-1
            word = word[1..word_length]+word[0]+"ay"
            string_array[i] = word
        end

        i += 1
    end
    return string_array.join(" ")
end
def translate(字符串)
string\u数组=string.split
字符串长度=字符串数组大小
i=0
当我
以下是测试失败消息:

失败:

 1) #translate translates two words
     Failure/Error: s.should == "eatay iepay"
       expected: "eatay iepay"
            got: "ateay epiay" (using ==)
     # ./04_pig_latin/pig_latin_spec.rb:41:in `block (2 levels) in <top (required)>'
1)#翻译两个单词
失败/错误:s.should==“eatay iepay”
预期:“eatay iepay”
获得:“ateay epiay”(使用==)
#/04_pig_拉丁语/pig_拉丁语_规范rb:41:in‘block(2层)in’
检查其他条件的附加代码用于我已经通过的其他测试。基本上,现在我用两个单词检查一个字符串

请让我知道如何使代码通过测试。提前谢谢你

“a”| |“e”| |“i”| |“o”| |“u”
计算为
“a”
,因为
“a”
是真值。(不是
nil
,不是
false
):

不如改用:

或者使用
=~
(正则表达式匹配):

“a”| |“e”| |“i”| |“o”| |“u”
计算为
“a”
,因为
“a”
是真值。(不是
nil
,不是
false
):

不如改用:

或者使用
=~
(正则表达式匹配):


%{aeiou}
应该是
%w{aeiou}
,不是吗
%{}
是一种模糊的创建字符串的方法(包括与
%w()
不同的所有空格字符)。@cremno,感谢您指出这一点。我相应地修改了代码。
%{aeiou}
应该是
%w{aeiou}
,不是吗
%{}
是一种模糊的创建字符串的方法(包括与
%w()
不同的所有空格字符)。@cremno,感谢您指出这一点。我相应地修改了代码。
irb(main):001:0> ("a" || "e" || "i" || "o" || "u")
=> "a"
irb(main):002:0> "a" == ("a" || "e" || "i" || "o" || "u")
=> true
irb(main):003:0> "e" == ("a" || "e" || "i" || "o" || "u")
=> false
irb(main):001:0> %w{a e i o u}.include? "a"
=> true
irb(main):002:0> %w{a e i o u}.include? "e"
=> true
irb(main):007:0> "e" =~ /[aeiou]/
=> 0