我需要一个Enumerator,它可以迭代从a到b - (b - a) / n.to_f()的n数字。我需要一个左闭的右开区间和步骤(b - a) / n.to_f()。我怎么才能拿到呢?
我使用a.step(b, (b - a) / n.to_f()),但是step返回一个在n + 1数字上迭代的Enumerator,包括两端a和b。
发布于 2014-09-26 22:08:27
def get_steps(a,b,n)
step = (b - a) / n.to_f
a.step(b - step, step) # or a.step(nil, step).take(n)
end
get_steps(2,4,5) #=> <Enumerator:... >
get_steps(2,4,5).to_a #=> [2.0, 2.4, 2.8, 3.2, 3.6]https://stackoverflow.com/questions/26068810
复制相似问题