看看rspec測試我再一次需要使用define創建一個方法,並讓它能夠使用多個參數。我想我需要將這些參數放入數組中。我不確定如何設置我的參數,以便它們進入數組並使參數無限,因此有人可以執行def sum(1,3,4,12,32,18,17,22)或添加或多或少。Ruby:如何將def參數放入數組中?
這是我的RSpec的測試,以確保它的工作原理
describe "sum" do
it "computes the sum of an empty array" do
sum([]).should == 0
end
it "computes the sum of an array of one number" do
sum([7]).should == 7
end
it "computes the sum of an array of two numbers" do
sum([7,11]).should == 18
end
it "computes the sum of an array of many numbers" do
sum([1,3,5,7,9]).should == 25
end
end
所以我的問題是如何獲取的定義方法輸入的參數爲數組?
不通過規範'[]' – Dty
已更新爲不帶參數傳遞。 – Casey