From: "Jesús Gabriel y Galán" Date: 2010-10-07T17:02:11+09:00 Subject: Re: save only first line from string? 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 line, >>> multiple lines, or no lines, and assume that we don't know which OS we >>> are on.) >>> -- >> >> Probably the most elegant, with Ruby 1.9: >> >> str = 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 entire > 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 = str.first This way you avoid the intermediate array. Jesus.