Project

General

Profile

Actions

Feature #9807

open

String.new with block

Added by citizen428 (Michael Kohl) almost 10 years ago. Updated almost 10 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:62414]

Description

After a discussion in our team chat today, I wondered if it would be a good idea to have a version of String.new that accepts a block and works as a string builder. Something like

string = String.new("foo") do |s|
  s << "bar"
end
string #=> "foobar"

If the argument is omitted an empty string would be passed to the block instead.

That could be a nice solution to avoid all the Array#join and "".tap hacks for string creation.

Actions

Also available in: Atom PDF

Like0
Like0Like0