From: Robert Dober Date: 2007-02-18T08:36:48+09:00 Subject: Re: interesting usages of Struct? On 2/17/07, Gregory Seidman wrote: > On Sun, Feb 18, 2007 at 12:00:15AM +0900, Phrogz wrote: > > On Feb 17, 1:39 am, "Robert Dober" wrote: > > > I was naming regular expressions with it roughly like this > > > > > > Fields=Struct.new(:a,:b,:c) > > > /(x*)(y*)(z*)/ === whatever > > > named= Fields.new( *Regexp.last_match.captures ) > > > > Ooh, that's quite cool. Thanks for sharing that! :) > > That's one way of doing it. There was a discussion not too long ago of > doing it with the MatchData itself. Something like (tested, works): > > class MatchData > def name_captures(*names) > meta = (class << self; self; end) > names.each_with_index { |name,index| > meta.send(:define_method, name) { captures[index] } > } > self > end > end > > named = /(x*)(y*)(z*)/.match(whatever).name_captures(:a, :b, :c) > > --Greg > > > That seems a little bit heavy. I was thinking to extend RegularExpressions so that the names could be bound to the Regexp for one and than just thought that I do not have enough usecases. AFAIK Ruby 2 will have named captures and I am not really smart enough to handle alternatives and nesting so I let it be... Cheers Robert -- We have not succeeded in answering all of our questions. In fact, in some ways, we are more confused than ever. But we feel we are confused on a higher level and about more important things. -Anonymous