From: Robert Klemme Date: 2011-08-29T23:44:12+09:00 Subject: Re: String#split resets regex captures variables (Ruby 1.8.7) On Mon, Aug 29, 2011 at 4:30 PM, Olivier Lance wrote: > thanks for all your replies and tests, I wasn't expecting so many > answers :) I am doing the opposite experience over at c.l.scheme - zero replies. So you're lucky to be on this side of the fence. > I'm sorry, my example was indeed a bit complicated to illustrate the > issue. The night got my brain, I should have slept ^^ :-) > Anyway, thanks for highlighting the different behaviors of those > variables... > I'm actually working on a code I haven't written, so I left it as > untouched as possible (as it was working) and patched with what I > needed. > I'll have a closer look to refactor it and use smarter/safer things. > > As I've said in my topic's subject, I'm using Ruby 1.8.7, so I think I > cannot use named groups. > What would you suggest instead of using $1 etc.? > > Should I store $~ and then use captures[1]...? Depends on the number of groups. If I want to be sure values are not changed I typically do something like if /../ =~ str name = $1 age = $2.to_i ... # use name and age end Or, if there are many groups you could do if /../ =~ str all, name, age, more, variables, here = *$~ ... # use name, age, more, variables, here end Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/