Gennady Bystritsky
3/5/2009 8:31:00 AM
> -----Original Message-----
> From: Leo [mailto:minilith@gmail.com]
> Sent: Wednesday, March 04, 2009 9:53 PM
> To: ruby-talk ML
> Subject: Re: Cut a string each 80 character
>=20
> > string.scan(%r{.{1,80}}).map { |_chunk|
> > =A0 _chunk + '<br />'
> > }
>=20
> Isn't that pretty much the same as:
>=20
> string.gsub(%r{.{1,80}}, "\\0<br />")
>=20
> leo.
Almost. The latter produces a string with "<br />" after every 80 chars, wh=
ile the former returns an array of strings 86 chars long each. What is more=
appropriate depends on a context. Say, you may want to decorate chunks eve=
n further by passing them through some other methods (on the other hand, gr=
ep with a block may be used for that as well).
The latter may be faster, though.=20
Thanks for bringing it up,
Gennady.