top | dibdib: Random things | programming: Ruby  /  EDF  /  Palm  /  Perl | obsessions: Games | about: me  /  my pages  /  people

named regexp parts

patch to Ruby 1.6.6 (RubyUnit test)

A patch to Ruby 1.6.6 that lets you do things like

  string = "text string for this"
  string =~ /(?[foo]st.*?) /
  puts "#{$foo}" # prints "string"

Caveats

I knocked this up in short space of time and it's an inelegant hack, but it mostly works. It only defines global variables because the first way I tried (rb_eval("variable=%q{match}") broke on things like "2{=" and it was easier to switch to rb_define_variable than to handle borderline cases like that.

It doesn't check that the variable you're passing is legal, valid, sensible or even vaguely presentable. It trusts you not to hurt it.

I'm not sure the memory management is sensible (I use strdup at one point, but I guess I could easily change that to use rb_str_new() and store the Ruby string instead of the C string. The only documentation I had to work from was README.EXT in the source tarball.

Variables are only wiped when the whole regexp fails, not individually, which is suboptimal but better than nothing for the moment.

TODO