Ruby / rails merge all elements between two indexes

I have such an array: [7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]

What is the easiest way to return each element in the array from position 6 to 0, where the resulting array looks like this: [1,2,3,4,5,6,7]

These positions in the array can be dynamic, for example, transmission in 4 and 9 should return [11,12,1,2,3,4]

I am wondering if there is a way that does this in the Rails api.

Thank you in advance

EDIT Suppose there are no negative numbers, so array[2..-2] does not work.

Array#splice almost works for this, but if the second position is less than the first, it returns nil .

+5
source share
7 answers
 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 do not want a monkey patch

You may 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] 
+2
source
 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] 
+3
source
 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 the ternary operator to make it one liner:

 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] 
+1
source
 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 
0
source
 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] 
0
source

A few more ways (my preference for # 1).

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

# 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

 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] 
0
source
 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] 
-1
source

All Articles