Forum: Ruby How to find at which line characters appear

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C5b20b09d5d24bd554f703ff0595696b?d=identicon&s=25 Al Cholic (desertfox)
on 2007-07-05 00:02
Hello,

I have a file that includes a "---------" string at a certain line
number.  How can I find out at which line number the string occurs?
Would it be best to split the text file into an array with split("\n")
and then use a for loop or is there some other hidden methods in ruby?

Basically I want to start reading a text file after the "---------" line
that appears in it.

Can someone help please?

Thanks in advance.
D046d3c8b40b45f19044671cf6cf40f9?d=identicon&s=25 Brett Simmers (Guest)
on 2007-07-05 00:50
(Received via mailing list)
f = File.open('filename')
until f.gets.chomp == "---------"; end
#continue processing here

That should do the trick.  The next line returned by f.gets will be the
first line after the "--------".  If you want the actual line number,
f.lineno should give it to you.

-Brett
Cf7cd97cdc8ed7d4ae92965b24f0dfad?d=identicon&s=25 Stefan Rusterholz (apeiros)
on 2007-07-05 01:08
Al Cholic wrote:
> Hello,
>
> I have a file that includes a "---------" string at a certain line
> number.  How can I find out at which line number the string occurs?
> Would it be best to split the text file into an array with split("\n")
> and then use a for loop or is there some other hidden methods in ruby?
>
> Basically I want to start reading a text file after the "---------" line
> that appears in it.
>
> Can someone help please?
>
> Thanks in advance.

2 methods come to my mind to do that:
1) as you said, split and find what index in the array contains the line
2) get the offset (String#index) of the first "---------", count the
"\n"s in the substring 0,offset.
I'd assume that the second is faster.

Regards
Stefan
Cf7cd97cdc8ed7d4ae92965b24f0dfad?d=identicon&s=25 Stefan Rusterholz (apeiros)
on 2007-07-05 01:13
Whoops, I should have read this paragraph too:

>> Basically I want to start reading a text file after the "---------" line
>> that appears in it.

Then of course I'd do it just as Brett already said: read until you get
there not processing the data, then continue to read, this time
processing the data.

Regards
Stefan
C5b20b09d5d24bd554f703ff0595696b?d=identicon&s=25 Al Cholic (desertfox)
on 2007-07-05 05:10
Stefan Rusterholz wrote:
> Whoops, I should have read this paragraph too:
>
>>> Basically I want to start reading a text file after the "---------" line
>>> that appears in it.
>
> Then of course I'd do it just as Brett already said: read until you get
> there not processing the data, then continue to read, this time
> processing the data.
>
> Regards
> Stefan


Thanks for guys' advice.  I also came up with another way to get the
text.

def find_component_list(text)
  /___+/.match(text).post_match.strip.split("\n")
end

Im using a regular expression.

How good is this in terms of efficiency compared to the other methods?
This topic is locked and can not be replied to.