From: Garance A Drosehn Date: 2005-08-02T05:52:58+09:00 Subject: Re: Defining regexp's and variables set by them On 8/1/05, Garance A Drosehn wrote: > > The downside to this is that the regular-expression is now > defined somewhere "far away" from the when-clause that > uses it. After some testing I may need to make changes to > the regexp, such as: > > re_simple = Regexp.new('(check|test) (\d+)') > > The thing is, by changing 'check' to '(check|test)', I have to > remember that the when clause also needs to change from > referencing $1 to referencing $2. [...] > > I was kinda wondering if it would make sense for ruby to > support something like: > > re_simple = Regexp.new('check (\d+)') { cnum = $1 } > > so I could then have the when clause say: > > when re_simple > check_number = cnum > > I realize this is a trivial example, but as the expressions get > more involved, and the case-statement has many when's, it > would be nice if I could have the compiled regular-expression > set values on variable names that *I* pick, in addition to the > standard values in a MatchData object. I thought about this some more after going home and getting some sleep... One obvious question is what would be the scope of the commands inside the { ...code-fragment...}. It also occurred to me that I sometimes I make a match, and then I pass around the resulting MatchData object to other methods, and *they* do things based on info in MatchData. So, I came up with this idea: Allow MatchData to include some user-settable value, which would initially be set to 'nil' at the time of the match. And then support: re_simple = Regexp.new('check (\d+)') { |mdata| mdata.userdata = mdata[1] } or: re_simple = Regexp.new('check (\d+)') { |mdata| mdata.userdata = Hash.new mdata.userdata["cnum"] = mdata[1] mdata.userdata["otherval"] = mdata[7] } That way, all the variables that the user is setting will be tied to the appropriate MatchData object. I almost think I could implement this by creating my own subclasses for Regexp and MatchData... -- Garance Alistair Drosehn = drosihn@gmail.com Senior Systems Programmer or gad@FreeBSD.org Rensselaer Polytechnic Institute; Troy, NY; USA