* [ruby-core:122612] [Ruby Feature#21455] Add a block argument to Array#join
@ 2025-06-28 0:35 leoarnold (Leo Arnold) via ruby-core
2025-07-08 7:52 ` [ruby-core:122675] " mame (Yusuke Endoh) via ruby-core
2025-07-10 8:45 ` [ruby-core:122710] " matz (Yukihiro Matsumoto) via ruby-core
0 siblings, 2 replies; 3+ messages in thread
From: leoarnold (Leo Arnold) via ruby-core @ 2025-06-28 0:35 UTC (permalink / raw)
To: ruby-core; +Cc: leoarnold (Leo Arnold)
Issue #21455 has been reported by leoarnold (Leo Arnold).
----------------------------------------
Feature #21455: Add a block argument to Array#join
https://bugs.ruby-lang.org/issues/21455
* Author: leoarnold (Leo Arnold)
* Status: Open
----------------------------------------
I sometimes come across code like this where
the `Array#join` at the end can easily
be overlooked or stands out like a sore thumb:
```ruby
hex_string = string.bytes.map do |byte|
format('%02X', byte)
end.join(' ')
```
It seems idiomatic and more succinct
to pass the block to `Array#join` directly:
```ruby
hex_string = string.bytes.join(' ') do |byte|
format('%02X', byte)
end
```
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 3+ messages in thread
* [ruby-core:122675] [Ruby Feature#21455] Add a block argument to Array#join
2025-06-28 0:35 [ruby-core:122612] [Ruby Feature#21455] Add a block argument to Array#join leoarnold (Leo Arnold) via ruby-core
@ 2025-07-08 7:52 ` mame (Yusuke Endoh) via ruby-core
2025-07-10 8:45 ` [ruby-core:122710] " matz (Yukihiro Matsumoto) via ruby-core
1 sibling, 0 replies; 3+ messages in thread
From: mame (Yusuke Endoh) via ruby-core @ 2025-07-08 7:52 UTC (permalink / raw)
To: ruby-core; +Cc: mame (Yusuke Endoh)
Issue #21455 has been updated by mame (Yusuke Endoh).
I have a different view on this proposal.
If `Array#join` were to accept a block, I would expect its behavior to relate directly to the "join" action itself, not the transformation of each element (which is the responsibility of `map`).
Specifically, it seems most appropriate for the block's return value to be used as the separator between elements. This would allow for dynamic separators based on context, such as the position of the join.
For example, this approach provides an elegant way to handle the Oxford comma:
```ruby
items = ["Apple", "Banana", "Cherry"]
i = 0
str = items.join do
i += 1
if i == items.size - 2
", and "
else
", "
end
end
p str #=> "Apple, Banana, and Cherry"
```
I believe this approach is more intuitive and consistent.
To be clear, I am not making a counter-proposal here. My main point is that it feels illogical for `Array#join` to accept a block that performs a `map`-like operation, as that behavior is unrelated to the behavior of "join" itself.
----------------------------------------
Feature #21455: Add a block argument to Array#join
https://bugs.ruby-lang.org/issues/21455#change-113955
* Author: leoarnold (Leo Arnold)
* Status: Open
----------------------------------------
I sometimes come across code like this where
the `Array#join` at the end can easily
be overlooked or stands out like a sore thumb:
```ruby
hex_string = string.bytes.map do |byte|
format('%02X', byte)
end.join(' ')
```
It seems idiomatic and more succinct
to pass the block to `Array#join` directly:
```ruby
hex_string = string.bytes.join(' ') do |byte|
format('%02X', byte)
end
```
Pull Request: https://github.com/ruby/ruby/pull/13731
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 3+ messages in thread
* [ruby-core:122710] [Ruby Feature#21455] Add a block argument to Array#join
2025-06-28 0:35 [ruby-core:122612] [Ruby Feature#21455] Add a block argument to Array#join leoarnold (Leo Arnold) via ruby-core
2025-07-08 7:52 ` [ruby-core:122675] " mame (Yusuke Endoh) via ruby-core
@ 2025-07-10 8:45 ` matz (Yukihiro Matsumoto) via ruby-core
1 sibling, 0 replies; 3+ messages in thread
From: matz (Yukihiro Matsumoto) via ruby-core @ 2025-07-10 8:45 UTC (permalink / raw)
To: ruby-core; +Cc: matz (Yukihiro Matsumoto)
Issue #21455 has been updated by matz (Yukihiro Matsumoto).
Status changed from Open to Rejected
It's fundamentally `join_map` we have rejected. Array#join should join, not mapping.
We are not going to add every enumerable method combined with map. `filter_map` is an exception.
Matz.
----------------------------------------
Feature #21455: Add a block argument to Array#join
https://bugs.ruby-lang.org/issues/21455#change-113987
* Author: leoarnold (Leo Arnold)
* Status: Rejected
----------------------------------------
I sometimes come across code like this where
the `Array#join` at the end can easily
be overlooked or stands out like a sore thumb:
```ruby
hex_string = string.bytes.map do |byte|
format('%02X', byte)
end.join(' ')
```
It seems idiomatic and more succinct
to pass the block to `Array#join` directly:
```ruby
hex_string = string.bytes.join(' ') do |byte|
format('%02X', byte)
end
```
Pull Request: https://github.com/ruby/ruby/pull/13731
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-07-10 8:48 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-06-28 0:35 [ruby-core:122612] [Ruby Feature#21455] Add a block argument to Array#join leoarnold (Leo Arnold) via ruby-core
2025-07-08 7:52 ` [ruby-core:122675] " mame (Yusuke Endoh) via ruby-core
2025-07-10 8:45 ` [ruby-core:122710] " matz (Yukihiro Matsumoto) via ruby-core
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).