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.)
Assuming you mean to actually modify the string, you could do this:
irb(main):001:0> "a string\nwith multiple\nlines".gsub! /\A(.*?)$(.*)\z/m, '\1'
=> "a string"
irb(main):002:0> "a string with one line".gsub! /\A(.*?)$(.*)\z/m, '\1'
=> "a string with one line"
irb(main):003:0> "".gsub! /\A(.*?)$(.*)\z/m, '\1'
=> ""
If you don't need to modify the string:
irb(main):004:0> "a string\nwith multiple\nlines".match(/\A.*?$/)[0]
=> "a string"
irb(main):005:0> "a string with one line".match(/\A.*?$/)[0]
=> "a string with one line"
irb(main):006:0> "".match(/\A.*?$/)[0]
=> ""
-Justin
···
On 10/03/2010 07:19 PM, 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.)
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.)
I don't know about the most elegant or simple way, but how about this:
string.sub!(/^(.*\n?)((?im).+)?/, '\1').chomp
Some tests:
string_1 = <<END_OF_STRING_1
hello there.
how is it going?
END_OF_STRING_1
p string_1.sub!(regex, '\1').chomp # => "hello there."
p string_2.sub!(regex, '\1').chomp # => "hi there"
p string_3.sub!(regex, '\1').chomp # => ""
p string_4.sub!(regex, '\1').chomp # => "hi"
p string_5.sub!(regex, '\1').chomp # => ""
I don't think that the ^ is really needed, but I included it anyway.
Also, don't be surprised if someone finds a fault with this or comes up
with a better solution (I never figured I would be giving advice on
regexs).
On Sun, Oct 3, 2010 at 9:19 PM, 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.)
--
Posted via http://www.ruby-forum.com/\.
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.)
"a string\nwith multiple\nlines".split(/\n|\r\n/)[0]
On Mon, Oct 4, 2010 at 4:19 AM, 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.)
"a string\nwith multiple\nlines"[ /.*/ ]
==>"a string"
···
On Oct 3, 9:19 pm, Terry Michaels <sp...@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.)
--
Posted viahttp://www.ruby-forum.com/.
add ".chomp" if you want to remove the trailing EOL.
···
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.)
--
On Mon, Oct 4, 2010 at 4:19 AM, 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.)
Oops, already found a problem. The chomp is not chomp! So it does not
cut the "\n" off of the string (if present). You can;t just put the ! on
the end of chomp because it returns nil if "\n" was not found.
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.)
I don't know about the most elegant or simple way, but how about this:
"a string\nwith multiple\nlines".split(/\n|\r\n/)[0]
=> "a string"
"a string\nwith multiple\nlines".split(/\r?\n/).first
I was somewhat amazed to find not the split solution, but the rather
wild (g)sub solutions as the first responses!
···
On Mon, Oct 4, 2010 at 8:25 AM, Steel Steel <angel_steel@ymail.com> wrote:
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.)
"a string\nwith multiple\nlines".split(/\n|\r\n/)[0]
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.
--
Posted via http://www.ruby-forum.com/\.
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.
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
···
2010/10/7 Jesús Gabriel y Galán <jgabrielygalan@gmail.com>
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.
String#lines returns an Enumerator, not an Array. I haven't looked at
its implementation, but it does not necessarily process the whole string
if you only call #first.