So I’ve been using the ‘jcode’ library to get UTF-8 support and wanted
to add a convenience method called “jindex” that would work with multi-
byte characters in much the same way that “jlength” does.
The basic idea is to call the real “index” method to do the work, and
then convert the returned byte-count index into a character-count
index before returning it to the user. This works fine, but the
problem is that I can’t find a way to propagate $~ back to the user.
That is, after calling this:
$~ is set and $~ is “foo”.
But when I do this.
$~ is nil. The following short irb session demonstrates what’s
happening in the two cases:
irb(main):001:0> class StringTest < String
irb(main):002:1> def index foo
irb(main):003:2> r = super
irb(main):006:0> StringTest.new(‘foo’).index /o/
NoMethodError: undefined method `’ for nil:NilClass
irb(main):008:0> String.new(‘foo’).index /o/
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/
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…
Are my worst fears right on this one?