0
此測試通過驗證,但是如何?Ruby與Rspec - 此測試如何通過?
#Rspec
it "handles a case with an answer > 1 distance to the left" do
nearest_larger([8,2,4,3], 2).should == 0
end
#Function
def nearest_larger(arr, idx)
diff = 1
loop do
left = idx - diff
right = idx + diff
if (left >= 0) && (arr[left] > arr[idx])
return left
elsif (right < arr.length) && (arr[right] > arr[idx])
return right
elsif (left > 0) && (right >= arr.length)
return nil
end
diff += 1
end
end
我正在通過一些ruby的例子,我不明白這個測試如何通過驗證。 0
從哪裏返回?
'left = idx-diff'' diff' = 1因此'left' =='1' – fyz
是的,編輯了我的回覆。 –
你我的朋友真棒。謝謝。我現在看到它!對評論+1。亞利桑那州的科技情況如何?我想搬出去。 – fyz