From: botp Date: 2011-01-17T21:38:20+09:00 Subject: Re: A tad of meta-programming needed On Mon, Jan 17, 2011 at 2:05 PM, Josh Cheek wrote: > On Sun, Jan 16, 2011 at 11:30 PM, RichardOnRails < > The matchdata object implements the same inteface as a hash. If that is all > you need, on 1.9 you could just name the capture groups, then use the > MatchData object and not have to do any assignment/creation at all. > (Assuming this data is based on the pastie you posted earlier, this code > would work) > RUBY_VERSION # => "1.9.2" > line = "Apple 1.1234 1/2/3 4/5/6 $1,234.56 $7 ($5)" > regex = /^(?       .+                       )\s >          (?   \d+\.\d{4}               )\s+ >          (? \d\d?\/  \d\d?\/  \d\d?  )\s+ >          (? \d\d?\/  \d\d?\/  \d\d?  )\s+ >          (?   [\$\d,\.]+               )\s+ >          (?       [\$\d,\.]+               )\s+ >          (?  [\(\)\$,\d \.]+          )\s* >        $/x > result = regex.match(line) or for older reg engines, they can do parallel assignments w array or splat something like _,:name,:n_shares,:opened = regex.match(line).to_a or _,:name,:n_shares,:opened = *regex.match(line) best regards -botp