From: mail@... Date: 2020-06-10T10:00:09+00:00 Subject: [ruby-core:98705] [Ruby master Feature#16946] Add an `intersperse` method Issue #16946 has been reported by sos4nt (Stefan Sch��ler). ---------------------------------------- Feature #16946: Add an `intersperse` method https://bugs.ruby-lang.org/issues/16946 * Author: sos4nt (Stefan Sch��ler) * Status: Open * Priority: Normal ---------------------------------------- Haskell has an `intersperse` function which adds a separator between elements of a list. It would be pretty useful to have such method(s) in Ruby, too. Examples for `Array` and `String`: ```ruby [1, 2, 3].intersperse(0) #=> [1, 0, 2, 0, 3] 'Hello'.intersperse('-') #=> "H-e-l-l-o" ``` I'm aware that I can achieve the above with built-in methods, but it's quite cumbersome: (requiring regular expressions / intermediate arrays) ```ruby [1, 2, 3].flat_map { |i| [i, 0] }[0...-1] #=> [1, 0, 2, 0, 3] 'Hello'.gsub(/(?<=.)./, '-\0') #=> "H-e-l-l-o" 'Hello'.chars.join('-') #=> "H-e-l-l-o" ``` -- https://bugs.ruby-lang.org/ Unsubscribe: