From: zverok.offline@... Date: 2017-12-12T14:28:36+00:00 Subject: [ruby-core:84220] [Ruby trunk Feature#14022] String#surround Issue #14022 has been updated by zverok (Victor Shepelev). @matz Basically, in **my** practice (I can't speak for everyone of course) chaining is almost always a better way to construct value than operators, or interpolation, or something. Mostly because it follows "natural" flow of data, and therefore makes code more maintainable. ```ruby # Not that much difference ary.join(',').surround('<', '>') "<#{ary.join(',')}>" # More difference: File.read('some/source/path.txt') .split("\n") .map(&:strip) .grep_v(/^; /) .join(" ; ") .surround('(', ')') "(#{File.read('some/source/path.txt') .split("\n") .map(&:strip) .grep_v(/^; /) .join(" ; ")})" # of course, any sane developer rewrites the latter a result = File.read('some/source/path.txt') .split("\n") .map(&:strip) .grep_v(/^; /) .join(" ; ") "(#{result})" ``` But, as for **me** I always become frustrated when I need a new var because my "chain of thought" is broken by absence of methods. So, if we want optimize for happiness... Well, that was the reason I fought for `yield_self` (still hate the name!), so in 2.5.0 you can do: ```ruby File.read('some/source/path.txt') .split("\n") .map(&:strip) .grep_v(/^; /) .join(" ; ") .yield_self { |res| "(#{res})" } ``` But for this really frequent case `surround()` still feels more elegant. ---------------------------------------- Feature #14022: String#surround https://bugs.ruby-lang.org/issues/14022#change-68342 * 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: