From: Greg Hurrell Date: 2007-02-26T19:10:05+09:00 Subject: Re: Writing a "String#jindex" method to do the same for "index" as "String#jlength" does for "length" On 25 feb, 18:16, "Greg Hurrell" wrote: > > So I guess this is a more general question. If something inside a > method sets $~, how do you propagate this back to the caller? There > was a thread called "How to preserve $~ when extending Regexp#match" > last year that touched on a similar issue (http://groups.google.com/ > group/comp.lang.ruby/browse_thread/thread/73ccf213a9c0802d/ > a93554cd466acb44). This leads me to believe that the > "special_local_set" function is being used under the hood by the > "index" method and there will be no way for my "jindex" method to > mimick its behaviour without also getting down into the C extension > level, something I am not too keen to do as it is probably a bit over > my head for now... For the record, I decided to bite the bullet and try writing my first C extension for Ruby. It works and uses rb_backref_get and rb_backref_set to ensure that $~ gets correctly propagated back to the caller. If anyone knows of a clean, pure-Ruby way to do this (without a C extension) please let me know as it would be better from a portability/deployment perspective. Cheers, Greg