From: "mame (Yusuke Endoh) via ruby-core" Date: 2025-07-08T07:52:30+00:00 Subject: [ruby-core:122675] [Ruby Feature#21455] Add a block argument to Array#join 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/