From: Steve Howell Date: 2010-03-30T13:40:11+09:00 Subject: Re: return number of spaces at the beginning of a line On Mar 29, 9:35 pm, Steve Howell wrote: > On Mar 29, 7:41 pm, "Jesse B." 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: > >     def num_leading_spaces(s) >       prefix = (s =~ /(\s*)/) >       $1.length >     end > >     puts num_leading_spaces('   hello') Oops, still not sure whether this is ideal, but this is better than my other version: def num_leading_spaces(s) s =~ /(\s*)/ $1.length end