From: Patrick Hurley Date: 2006-06-28T11:21:39+09:00 Subject: Re: string manipulation On 6/27/06, Jeff Pritchard wrote: > My question is, is String#scan(/./) the best way to convert a string > into an array so that one could then use Array#each or > Array#each_with_index to mess with the characters? It seems to work in > irb, but I'm guessing there are some corner cases where the regexp would > falter. Also seems more computationally intensive that should be > necessary to just split it up into characters. Try String#each_byte and then .chr the numeric byte value. > Also, my first thought on a solution for this was to simply walk along > the string and insert dashes where appropriate. Why isn't there a > "String#insert(where,str)" method? Inserting substrings into strings > seems like such a common thing to do. You mean like String#insert :-) it is there, of course there is no insert!, so you have to remember to save the result. > And finally, a question about Alex's gsub. I don't quite follow the > "\\0-" syntax. It appears related to the group matching discussed on > page 326 of pickaxe, but not quite. Clearly in the "\\0-" sequence we > have the first slash escaping the second one, so it is effectively \0 > plus the '-', in other words replace the four characters matched with > the four characters plus a dash. What I don't get is how \0 outside of > the pattern is giving us anything useful. Seems like it should be $1 > instead, since it is outside of the regular expression being matched. > But then, there aren't any parenthesis, so there is no group, so perhaps > $1 doesn't apply either. Can someone offer some clarification? This is as documented and an artifact of Ruby's history (i.e. a Perlism), which in turn was a sedism, which was brought in from ed... pth