From: Josh Cheek Date: 2010-10-07T19:00:33+09:00 Subject: Re: save only first line from string? --0016364d3059bfec98049203ec38 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 2010/10/7 Jes=FAs Gabriel y Gal=E1n > On Thu, Oct 7, 2010 at 7:44 AM, Terry Michaels > wrote: > > Benoit Daloze wrote: > >> On 4 October 2010 04:19, Terry Michaels wrote: > >>> Hi. What's the most simple and elegant way to remove all the contents > of > >>> a String except for the first line? (Assume string consist of one lin= e, > >>> multiple lines, or no lines, and assume that we don't know which OS w= e > >>> are on.) > >>> -- > >> > >> Probably the most elegant, with Ruby 1.9: > >> > >> str =3D str.lines.first > >> > >> add ".chomp" if you want to remove the trailing EOL. > > > > Definitely the most elegant! (Defining 'elegant' as simple, natural, > > easy to read) Thanks! This seems to work fine on my system with ruby > > 1.8.7 (patchlevel 302). Possibly not the most efficient (requires entir= e > > string to be split into array elements) but okay in my case. > > > > In fairness to the others, I should have explained that I was okay with > > replacing the old string with a new string. > > Did my solution work for you? > > str =3D str.first > > This way you avoid the intermediate array. > > Jesus. > > I tried it on 1.8.6, 1.8.7, 1.9.1, 1.9.2, and found that it only worked on 1.8.7 --0016364d3059bfec98049203ec38--