[ruby-core:62416] [ruby-trunk - Feature #9807] String.new with block

From: matthew@...
Date: 2014-05-06 11:38:52 UTC
List: ruby-core #62416
Issue #9807 has been updated by Matthew Kerwin.


 >
 > That could be a nice solution to avoid all the Array#join and "".tap
 hacks for string creation.
 >
=20
 Which hacks are these? Also, I don't see how it's different from
 =C2=B4"foo".tap{|s|s<<"bar"}=C2=B4
=20
 Can you give some examples?

----------------------------------------
Feature #9807: String.new with block
https://bugs.ruby-lang.org/issues/9807#change-46566

* Author: Michael Kohl
* Status: Open
* Priority: Normal
* Assignee:=20
* Category:=20
* Target version:=20
----------------------------------------
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 s=
tring builder. Something like

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

If the argument is omitted an empty string would be passed to the block ins=
tead.

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



--=20
https://bugs.ruby-lang.org/

In This Thread

Prev Next