我有一系列数字.我想将它转换为范围数组.例:
input = [0,10,20,30]
output = [0..10, 10..20, 20..30, 30..Infinity]
Run Code Online (Sandbox Code Playgroud)
有没有直接的方法在Ruby中做到这一点?
是的,可能:
input = [0,10,20,30]
(input + [Float::INFINITY]).each_cons(2).map { |a,b| a..b }
# => [0..10, 10..20, 20..30, 30..Infinity]
Run Code Online (Sandbox Code Playgroud)