Project

General

Profile

Feature #9807

String.new with block

Added by citizen428 (Michael Kohl) about 6 years ago. Updated about 6 years ago.

Status:
Open
Priority:
Normal
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.

Also available in: Atom PDF