From: Arun Kumar Date: 2009-04-30T16:48:27+09:00 Subject: Re: Random Access using IO#pos in code blocks --001e680f1328c3885c0468c0eb88 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Thank you so much for all the responses :-) On Thu, Apr 30, 2009 at 12:37 PM, Brian Candler wrote= : > Robert Klemme wrote: > > String#scan is likely faster than manually matching portions with > > #match. In both versions of Ruby you can do this to get the > > /character/ offset: > > > > irb(main):001:0> s=3D%{foo bar baz} > > =3D> "foo bar baz" > > irb(main):002:0> s.scan(/\w+/) { p $`.length } > > 0 > > 4 > > 8 > > =3D> "foo bar baz" > > Well, my guess is that would be *less* efficient for large paragraphs, > since $` forces allocation of a new string containing all the text from > the start to the current point. But that reminds me, there is a global > variable containing a MatchData object: $~ > > So you can write: > > irb(main):001:0> s=3D%{foo bar baz} > =3D> "foo bar baz" > irb(main):002:0> s.scan(/\w+/) { p $~.begin(0) } > 0 > 4 > 8 > =3D> "foo bar baz" > > Regards, > > Brian. > -- > Posted via http://www.ruby-forum.com/. > > --=20 || =E0=A4=B6=E0=A5=8D=E0=A4=B0=E0=A5=80 =E0=A4=9C=E0=A4=BE=E0=A4=A8=E0=A4= =95=E0=A5=80=E0=A4=B0=E0=A4=98=E0=A5=81=E0=A4=A8=E0=A4=BE=E0=A4=A5=E0=A5=8B= =E0=A4=B5=E0=A4=BF=E0=A4=9C=E0=A4=AF=E0=A4=A4=E0=A5=87 || --001e680f1328c3885c0468c0eb88--