Issue #15024 has been updated by jeremyevans0 (Jeremy Evans).


Your examples are both possible to implement using existing Array methods:

~~~ ruby
puts %w{a b c d}.each_slice(2).map{|a| a.join(", ")}.join("\n")
a, b
c, d

%w{a b c d}.each_cons(2).with_index.each{|a, i| puts (a << i).join(":")}
a:b:0
b:c:1
c:d:2
~~~

In the first case, your example has the block return the join separator.  In the second example you don't appear to be returning the separator, you appear to be using the block for the side effect of iterating over the equivalent of `each_cons(2).with_index` (with before and after as separate block arguments).

I don't think adding block support to Array#join improves either example, and I think it makes the code more difficult to understand.

----------------------------------------
Feature #15024: Support block in Array#join
https://bugs.ruby-lang.org/issues/15024#change-73694

* Author: graywolf (Gray Wolf)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I think it could be handy to have block support in Array#join.

For example

```ruby
> puts %w{a b c d}.join { |_, _, i| i % 2 == 1 ? "\n" : ', ' }
a, b
c, d
```

not sure what arguments exactly the block should take, atm I'm thinking
of

```ruby
> %w{a b c d}.join { |before, after, i| puts "#{before}:#{after}:#{i}" }
a:b:0
b:c:1
c:d:2
```

Would appreciate some feedback before I try putting together patch for this.




-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>