From: Robert Klemme Date: 2013-04-18T23:15:38+09:00 Subject: Re: String index produces unanticipated result --047d7bdc0c4e81173104daa33cb5 Content-Type: text/plain; charset=ISO-8859-1 On Wed, Apr 17, 2013 at 10:41 PM, Thomas Luedeke wrote: > Thanks. I am running Ruby 1.8.7. That would explain why online irb gave > me what I expected. > > What I used that worked was line[0..4].strip[0..0]. Makes me cringe, > but it does work. > Why then don't you do something like this: irb(main):005:0> s=" 9 3 3 0 0" => " 9 3 3 0 0" irb(main):006:0> s.scan(/\d+/).map(&:to_i)[0, 5].include? 9 => true For 1.8 irb(main):007:0> s.scan(/\d+/).map {|m| m.to_i}[0, 5].include? 9 => true Or, with a regex irb(main):009:0> /\A\s*(?:\d+\s+){0,4}9\D/ =~ s => 0 Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ --047d7bdc0c4e81173104daa33cb5 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable



On Wed, Apr 17, 2013 at 10:41 PM, Thomas Luedeke = <lists@ruby-fo= rum.com> wrote:
Thanks. I am running Ruby 1.8.7. =A0That would explain why= online irb gave
me what I expected.

What I used that worked was line[0..4].strip[0..0]. =A0 Makes me cringe, but it does work.

Why then don= 9;t you do something like this:

i= rb(main):005:0> s=3D" =A0 =A09 =A0 =A03 =A0 =A03 =A0 =A00 =A0 =A00&= quot;
=3D> " =A0 =A09 =A0 =A03 =A0 =A03 =A0 =A00 =A0 =A00= "
irb(main):006:0> s.scan(/\d+/).map(&:to_i)[0, 5].include? 9
=3D> true

For 1.8
=
irb(main):007:0> s.scan(/\d+/).map {|m| m.to_i= }[0, 5].include? 9
=3D> true

Or, with a regex

irb(main):009:0> /\A\s*(?:\d+\s+){0,4}9\D/ = =3D~ s
=3D> 0

Kind regards

robert


--
remember.guy do |as, often| as.you_can - without end
= http://blog.rubybestpractice= s.com/
--047d7bdc0c4e81173104daa33cb5--