我在寫測試文件,但我不能把它通過第二次測試,在這裏:如何在Ruby中檢查字符串中的第一個字母?
def translate(word)
if word.start_with?('a','e','i','o','u')
word << "ay"
else
word << "bay"
end
end
是做這份工作的合適方法?
describe "#translate" do
it "translates a word beginning with a vowel" do
s = translate("apple")
s.should == "appleay"
end
it "translates a word beginning with a consonant" do
s = translate("banana")
s.should == "ananabay"
end
it "translates a word beginning with two consonants" do
s = translate("cherry")
s.should == "errychay"
end
end
編輯: 我的解決方案不完整。 我的代碼只能通過第一次測試,因爲我能夠將「ay」推到單詞的結尾。我錯過了第二個測試是刪除第一個字母,如果它的輔音,在「香蕉」中是「b」。
爲什麼如果是輔音,第一個字母會被刪除?你的代碼不這樣做,因此測試不通過。 –
您是否在'translate'方法中發佈了'if'表達式?測試如何失敗? – toro2k
@ toro2k是的我已經def def(word)... end – egyamado