使用Rubymonk学习Ruby我不知道';无法理解,请运行代码块N次
Rubymonk为您提供了一段代码:使用Rubymonk学习Ruby我不知道';无法理解,请运行代码块N次,ruby,control-flow,Ruby,Control Flow,Rubymonk为您提供了一段代码: # add a loop inside this method to ring the bell 'n' times def ring(bell, n) bell.ring end 对于解决方案,您可以看到: def ring(bell, n) n.times do bell.ring end end 我知道循环在做什么,但我不知道方法的第一个参数做什么,也不知道如何使用正确的参数调用方法 bell只是一个带有环的对
# add a loop inside this method to ring the bell 'n' times
def ring(bell, n)
bell.ring
end
对于解决方案,您可以看到:
def ring(bell, n)
n.times do
bell.ring
end
end
我知道循环在做什么,但我不知道方法的第一个参数做什么,也不知道如何使用正确的参数调用方法
bell
只是一个带有环的对象
方法
例如:
class MyBell
def ring
print "***ring***\n"
end
end
def ring(bell, n)
n.times do
bell.ring
end
end
b = MyBell.new()
ring(b, 4)