On Thu, Oct 7, 2010 at 7:44 AM, Terry Michaels <spare / frigidcode.com> wrote:
> Benoit Daloze wrote:
>> On 4 October 2010 04:19, Terry Michaels <spare / frigidcode.com> 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.