How to insert a new element between all elements of a Ruby array?

I have an array and you want to insert a new element between all elements, somehow, like the join method. For example, I have

 [1, [], "333"] 

they do not need

 [1, {}, [], {}, "333"] 

Note that a new empty hash was inserted between all elements.

Edit: Currently I have:

 irb(main):028:0> a = [1, [], "333"] => [1, [], "333"] irb(main):029:0> a = a.inject([]){|x, y| x << y; x << {}; x} => [1, {}, [], {}, "333", {}] irb(main):030:0> a.pop => {} irb(main):031:0> a => [1, {}, [], {}, "333"] irb(main):032:0> 

I want to know the best way.

+6
source share
8 answers
 [1, 2, 3].flat_map { |x| [x, :a] }[0...-1] #=> [1, :a, 2, :a, 3] 

FYI, this function is called intersperse (at least in Haskell).

[Refresh] If you want to avoid a slice (which created a copy of the array):

 [1, 2, 3].flat_map { |x| [x, :a] }.tap(&:pop) #=> [1, :a, 2, :a, 3] 
+14
source
 a = [1,2,3] h, *t = a r = [h] t.each do |e| r.push({}, e) end r #=> [1, {}, 2, {}, 3] 
+1
source

You can do something like:

 a = [1, [], "333"] new_a = a.collect {|e| [e, {}]}.flatten(1) => [1, {}, [], {}, "333", {}] 

You need to do .flatten(1) because it will smooth your empty array without it.

Or, as @David Grayson says in a comment, you can do a flat_map that will do the same.

 a.flat_map {|e| [e, {}]} => [1, {}, [], {}, "333", {}] 

@tokland has the correct answer if the last {} is not needed. You return a slice from 0 to length - 1 or [0..-1] .

+1
source

Another similar solution uses #product :

 [1, 2, 3].product([{}]).flatten(1)[0...-1] # => [ 1, {}, 2, {}, 3 ] 
+1
source
 irb(main):054:0* [1, 2, 3, 4, 5, 6, 7, 8, 9].each_slice(1).flat_map {|e| e << "XXX"}[0...-1] => [1, "XXX", 2, "XXX", 3, "XXX", 4, "XXX", 5, "XXX", 6, "XXX", 7, "XXX", 8, "XXX", 9] irb(main):055:0> [1, 2, 3, 4, 5, 6, 7, 8, 9].each_slice(2).flat_map {|e| e << "XXX"}[0...-1] => [1, 2, "XXX", 3, 4, "XXX", 5, 6, "XXX", 7, 8, "XXX", 9] irb(main):056:0> [1, 2, 3, 4, 5, 6, 7, 8, 9].each_slice(3).flat_map {|e| e << "XXX"}[0...-1] => [1, 2, 3, "XXX", 4, 5, 6, "XXX", 7, 8, 9] irb(main):057:0> [1, 2, 3, 4, 5, 6, 7, 8, 9].each_slice(4).flat_map {|e| e << "XXX"}[0...-1] => [1, 2, 3, 4, "XXX", 5, 6, 7, 8, "XXX", 9] irb(main):058:0> [1, 2, 3, 4, 5, 6, 7, 8, 9].each_slice(5).flat_map {|e| e << "XXX"}[0...-1] => [1, 2, 3, 4, 5, "XXX", 6, 7, 8, 9] irb(main):059:0> [1, 2, 3, 4, 5, 6, 7, 8, 9].each_slice(6).flat_map {|e| e << "XXX"}[0...-1] => [1, 2, 3, 4, 5, 6, "XXX", 7, 8, 9] irb(main):060:0> [1, 2, 3, 4, 5, 6, 7, 8, 9].each_slice(7).flat_map {|e| e << "XXX"}[0...-1] => [1, 2, 3, 4, 5, 6, 7, "XXX", 8, 9] irb(main):061:0> [1, 2, 3, 4, 5, 6, 7, 8, 9].each_slice(8).flat_map {|e| e << "XXX"}[0...-1] => [1, 2, 3, 4, 5, 6, 7, 8, "XXX", 9] irb(main):062:0> [1, 2, 3, 4, 5, 6, 7, 8, 9].each_slice(9).flat_map {|e| e << "XXX"}[0...-1] => [1, 2, 3, 4, 5, 6, 7, 8, 9] irb(main):063:0> 
0
source

Another, similar to Tokland:

 xs.inject([]){|x,y| x << y << {}}[0...-1] 
0
source

One approach is to pin another array of desired elements and then smooth it with depth = 1 :

 > arr = [1, [], "333"] > element = {} > interspersed = arr.zip([element] * (arr.size - 1)).flatten(1).compact > # [1, {}, [], {}, "333" ] 

You can extend Array to make this behavior more accessible.

 class Array def intersperse(elem) self.zip([elem] * (self.size - 1)).flatten(1).compact end end 

eg,

[43] pry (main)> [1,2,3] .intersperse ('a')
=> [1, "a", 2, "a", 3]

0
source
 [1, 2, 3, 4, 5].inject { |memo, el| Array(memo) << {} << el } #=> [1, {}, 2, {}, 3, {}, 4, {}, 5] 

inject will use the first element to start with, so you don't need to mess with indexes.

0
source

Source: https://habr.com/ru/post/909222/


All Articles