简体   繁体   English

ruby / rails数组两个索引之间的所有元素

[英]ruby/rails array all elements between two indices

I have an array like this: [7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6] 我有这样一个数组: [7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]

What's the simplest way to return each item in the array from position 6 until 0 where the resulting array looks like: [1,2,3,4,5,6,7] 返回数组中每个项目从位置6到0的最简单方法是什么,结果数组如下所示: [1,2,3,4,5,6,7]

This positions in the array can be dynamic, for example passing in 4 and 9 should return [11,12,1,2,3,4] 数组中的这个位置可以是动态的,例如传入4和9应该返回[11,12,1,2,3,4]

I'm wondering if there's a method that accomplishes this in Rails api. 我想知道是否有一种方法可以在Rails api中实现这一点。

Thanks in advance 提前致谢

EDIT Let's assume that no negative numbers, so doing array[2..-2] wont work. 编辑让我们假设没有负数,所以做array[2..-2]不会工作。

Array#splice almost works for this, but if the second position is less than the first, it returns nil . Array#splice几乎适用于此,但如果第二个位置小于第一个位置,则返回nil

def foo a, min, max
  a.rotate(min).first((max - min) % a.length + 1)
end

a = [7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]
foo(a, 6, 0) # => [1, 2, 3, 4, 5, 6, 7]
foo(a, 4, 9) # => [11, 12, 1, 2, 3, 4]
class Array
   def get_sub_array(start,last)
       (start > last) ? (self[start..-1] + self[0..last]) : self[start..last]
   end
end

Then 然后

a = [7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]
a.get_sub_array(6,0)
#[1, 2, 3, 4, 5, 6, 7]

Or if you don't want to monkey patch 或者,如果你不想猴子补丁

You could have a method like 你可以有一个类似的方法

def get_sub_array(array, start,last)
   (start > last) ? (array[start..-1] + array[0..last]) : array[start..last]
end

a = [7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]
get_sub_array(a,6,0)
#[1, 2, 3, 4, 5, 6, 7]
def some_function(some_array,start_val=6, end_val=0)
  if end_val > start_val
    some_array[start_val,(end_val - start_val)]
  else
    (some_array[start_val, some_array.size] << some_array[0, (end_val)]).flatten
  end
end

You can use ternary operator to make it one liner too: 您也可以使用三元运算符使其成为一个衬管:

def some_function(some_array,start_val=6, end_val=0)
  end_val > start_val ? some_array[start_val,(end_val - start_val)] : (some_array[start_val, some_array.size] << some_array[0, (end_val)]).flatten
end

a = [7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]
some_function(a) # => [1, 2, 3, 4, 5, 6, 7]
some_function(a, 4, 9) # => [11, 12, 1, 2, 3, 4]
min=6
max=0
arr = [7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]
result = []
if max<min
  result << arr[min..arr.length]
  result << arr[0..max]
else
  result << arr[min..max]
end
def foo a, s, e
  a = e < s ? (a[s,a.size] << a[0..e]).flatten : a[s..e]
end

a = [7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]
a = foo(a, 6, 0) # => [1, 2, 3, 4, 5, 6, 7]
a = foo(a, 4, 9) # => [11, 12, 1, 2, 3, 4]

A couple more ways (my preference being for #1). 还有几种方式(我倾向于#1)。

a = [7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]

#1 #1

def foo a, min, max
  as = a.size
  max += as if max < min
  (min..max).map { |i| a[i%as] }
end

foo(a, 6, 0) # => [ 1,  2, 3, 4, 5, 6, 7]
foo(a, 4, 9) # => [11, 12, 1, 2, 3, 4]

#2 #2

def foo a, min, max
  max += a.size if max < min
  e = a.cycle
  min.times { e.next }
  (max-min+1).times.map { e.next }
end

foo(a, 6, 0) # => [ 1,  2, 3, 4, 5, 6, 7]
foo(a, 4, 9) # => [11, 12, 1, 2, 3, 4]
myArray = [7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]
myArray[6..-1] returns [1, 2, 3, 4, 5, 6]
myArray[4..9] returns [11,12,1,2,3,4]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM