让我们说我希望拥有无限的路径段,并将它们相乘,以便:
get "/multiply/num1/num2/num3......" do
num1 = params[:num1].to_i
num2 = params[:num2].to_i
....
solution = num1 * num2 * ....
"the solution is = #{solution}"
end
我希望用户能够输出任意数量的路径段,然后将这些数字的解决方案相乘。
答案 0 :(得分:1)
我找到了办法:
get "/multiply/*" do
n = params[:splat][0].split('/')
for i in (0...n.length)
n[i] = n[i].to_f
end
n = n.inject{ |sum, n| sum * n }
"solution = #{n}"
end