Writing a "String#jindex" method to do the same for "index" as "String#jlength" does for "length"

Discussion in 'Ruby' started by Greg Hurrell, Feb 25, 2007.

  1. Greg Hurrell

    Greg Hurrell Guest

    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:

    'foo'.index /foo/

    $~ is set and $~[0] is "foo".

    But when I do this.

    'foo'.jindex /foo/

    $~ 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):004:2> end
    irb(main):005:1> end
    => nil
    irb(main):006:0> StringTest.new('foo').index /o/
    => 1
    irb(main):007:0> $~[0]
    NoMethodError: undefined method `[]' for nil:NilClass
    from (irb):7
    from :0
    irb(main):008:0> String.new('foo').index /o/
    => 1
    irb(main):009:0> $~[0]
    => "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/
    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...

    Are my worst fears right on this one?

    Cheers,
    Greg
     
    Greg Hurrell, Feb 25, 2007
    #1
    1. Advertising

  2. Greg Hurrell

    Greg Hurrell Guest

    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
     
    Greg Hurrell, Feb 26, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mitchua
    Replies:
    5
    Views:
    2,776
    Eric J. Roode
    Jul 17, 2003
  2. HNguyen
    Replies:
    4
    Views:
    2,422
    HNguyen
    Dec 21, 2004
  3. darrel
    Replies:
    2
    Views:
    478
    darrel
    Apr 5, 2006
  4. id10t error
    Replies:
    3
    Views:
    739
    Alexey Smirnov
    Aug 31, 2009
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    321
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page