How to return part of an array in Ruby?

With a list in Python, I can return part of it using the following code:

foo = [1,2,3,4,5,6] bar = [10,20,30,40,50,60] half = len(foo) / 2 foobar = foo[:half] + bar[half:] 

Since Ruby does everything in arrays, I wonder if there is anything like that.

+96
arrays list ruby slice
Mar 29 '09 at 20:16
source share
6 answers

Yes, in Ruby, the syntax to slice an array is very similar to Python. Here is the ri documentation for the array index method:

 --------------------------------------------------------------- Array#[] array[index] -> obj or nil array[start, length] -> an_array or nil array[range] -> an_array or nil array.slice(index) -> obj or nil array.slice(start, length) -> an_array or nil array.slice(range) -> an_array or nil ------------------------------------------------------------------------ Element Reference---Returns the element at index, or returns a subarray starting at start and continuing for length elements, or returns a subarray specified by range. Negative indices count backward from the end of the array (-1 is the last element). Returns nil if the index (or starting index) are out of range. a = [ "a", "b", "c", "d", "e" ] a[2] + a[0] + a[1] #=> "cab" a[6] #=> nil a[1, 2] #=> [ "b", "c" ] a[1..3] #=> [ "b", "c", "d" ] a[4..7] #=> [ "e" ] a[6..10] #=> nil a[-3, 3] #=> [ "c", "d", "e" ] # special cases a[5] #=> nil a[6, 1] #=> nil a[5, 1] #=> [] a[5..10] #=> [] 
+152
Mar 29 '09 at 20:23
source share
β€” -

If you want to split / cut an array at index i,

 arr = arr.drop(i) > arr = [1,2,3,4,5] => [1, 2, 3, 4, 5] > arr.drop(2) => [3, 4, 5] 
+25
Feb 13 '14 at 5:23
source share

You can use slice () for this:

 >> foo = [1,2,3,4,5,6] => [1, 2, 3, 4, 5, 6] >> bar = [10,20,30,40,50,60] => [10, 20, 30, 40, 50, 60] >> half = foo.length / 2 => 3 >> foobar = foo.slice(0, half) + bar.slice(half, foo.length) => [1, 2, 3, 40, 50, 60] 

By the way, as far as I know, Python β€œlists” just efficiently implement dynamically growing arrays. The insert at the beginning is in O (n), the insert at the end is depreciated by O (1), random access is O (1).

+15
Mar 29 '09 at 20:20
source share

Another way is to use the range method

 foo = [1,2,3,4,5,6] bar = [10,20,30,40,50,60] a = foo[0...3] b = bar[3...6] print a + b => [1, 2, 3, 40, 50 , 60] 
+6
Jan 30 '15 at 12:06
source share

I like the ranges for this:

 def first_half(list) list[0...(list.length / 2)] end def last_half(list) list[(list.length / 2)..list.length] end 

However, be very careful whether the endpoint is included in your range. This becomes critical on an odd-length list, where you need to choose where you are going to break through the middle. Otherwise, you will end up counting the middle element twice.

The above example will sequentially put the middle element in the last half.

0
Mar 23 '18 at 19:26
source share

you can also write something like this

 foo = [1,2,3,4,5,6,7] bar = [10,20,30,40,50,60,70] half = foo.length / 2 foobar = (foo.first half) + (bar.drop half) => [1, 2, 3, 40, 50, 60, 70] 
-four
Mar 27 '11 at 18:33
source share



All Articles