On Mar 29, 9:35 ¨Βν¬ ΣτεφΘοχεμμ Όσθοχεμ®®®ΐωαθοο®γονχςοτεΊ > On Mar 29, 7:41 ¨Βν¬ ΆΚεσσε Β®Όκεσσε®®®ΐαομ®γονΎ χςοτεΊ > > > 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