0
我有一個關於定義我的主要問題的問題是我對這個參數如何工作有點困惑。Ruby:define的默認值?
這是我的方法
def repeat(repeated_word)
@repeated_word = repeated_word
"#@repeated_word #@repeated_word"
end
這是我的RSpec的測試,以確保我的方法工作。
describe "repeat" do
it "should repeat" do
repeat("hello").should == "hello hello"
end
# Wait a second! How can you make the "repeat" method
# take one *or* two arguments?
#
# Hint: *default values*
it "should repeat a number of times" do
repeat("hello", 3).should == "hello hello hello"
end
end
它通過了第一次測試但沒通過第二次測試。我的困惑是,如果我添加第二個參數意思def重複(repeat_word,times_repeated) 第一次測試然後失敗,因爲它有錯誤的參數數量。不知道如何設置默認值?