Robert Klemme
8/10/2007 11:07:00 AM
2007/8/10, Ak 756 <macro.peng@gmail.com>:
> Konrad Meyer wrote:
> > On Friday 10 August 2007 02:09:56 am Konrad Meyer wrote:
> >> my_data = $1
> > Better yet:
> > READ_BYTES = 16
> > content = File.open('foo'){|f| f.read }
> > my_data = content[content.index("find this string"), READ_BYTES]
But be careful because #index returns the starting position of the
string searched for.
> > # I tried to combine Lutz's and my earlier ideas
> >
> > Though this of course assumes the string is found.
>
> I don't care about huge file at present and this method works for me.
> Thanks Konrad and Lutz.
You can even do it in one line:
bytes = File.read("foo")[/your_string(.{10})/, 1]
This reads the file into one String, does one regexp match and returns
contents of the capturing group which in this case contains arbitrary
10 characters (i.e. bytes).
Kind regards
robert