Issue #14022 has been updated by Eregon (Benoit Daloze).


knu (Akinori MUSHA) wrote:
> I thought `yield_self` was about solving problems like this:
> 
> `[1, 2, 3].join(", ").yield_self { |s| "<#{s}>" }`
> 
> A nice-to-have in addition would be a [shorter name](https://bugs.ruby-lang.org/issues/10095), a [special syntax](https://bugs.ruby-lang.org/issues/6684#note-2), or a default block parameter (`it`, `_`, or whatever).

Interesting idea.
It is very long though.
It also is not as expressive as .surround("<", ">"), which makes the intent easier to read in my opinion.

----------------------------------------
Feature #14022: String#surround
https://bugs.ruby-lang.org/issues/14022#change-67391

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
After joining the elements of an array into a string using `Array#join`, I frequently need to put substrings before and after the string. In such case, I would have to use either of the following:

```ruby
[1, 2, 3].join(", ").prepend("<").concat(">") # => "<1, 2, 3>"
"<#{[1, 2, 3].join(", ")}>"                   # => "<1, 2, 3>"
"<" + [1, 2, 3].join(", ") + ">"              # => "<1, 2, 3>"
```

but none of them is concise enough. I wish there were `String#surround` that works like this:

```ruby
[1, 2, 3].join(", ").surround("<", ">") # => "<1, 2, 3>"
```



-- 
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>