我有一个关于定义的问题我的主要问题是我对参数是如何工作的有点困惑。
这是我的方法
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(repeat_word,times_repeated),那么第一个测试就会失败,因为它有错误的参数数量。不确定如何设置默认值?
发布于 2013-11-25 09:20:29
def repeat(repeated_word, repeats=2)
repeats.times.map { repeated_word }.join(' ')
endhttps://stackoverflow.com/questions/20183361
复制相似问题