On Mar 29, 9:35=A0pm, Steve Howell <showel... / yahoo.com> wrote:
> On Mar 29, 7:41=A0pm, "Jesse B." <jesse... / aol.com> wrote:
>
> > How would I find the number of spaces at the beginning of a line before
> > the occurrance of the first non-space character?
>
> > Would the best method be to use a regular expression that covers all
> > non-space characters and get the index of the first occurrance of that?
>
> I think the following expresses what you are trying to do, but I do
> not know if this is the most efficient or idiomatic way to do it:
>
> =A0 =A0 def num_leading_spaces(s)
> =A0 =A0 =A0 prefix =3D (s =3D~ /(\s*)/)
> =A0 =A0 =A0 $1.length
> =A0 =A0 end
>
> =A0 =A0 puts num_leading_spaces(' =A0 hello')

Oops, still not sure whether this is ideal, but this is better than my
other version:

    def num_leading_spaces(s)
      s =3D~ /(\s*)/
      $1.length
    end