Problem with aliasing String#index

Discussion in 'Ruby' started by Niklas Frykholm, Mar 15, 2005.

  1. Aliasing String#index:

    "rollo".index(/../, 1)
    puts "noalias #{$~}"

    class String
    alias old_index index

    def index(*arg)
    ret = old_index(*arg)
    puts "inside #{$~}"
    return ret
    end
    end

    "winni".index(/../, 3)
    puts "outside #{$~}"

    gives

    noalias ol
    inside ni
    outside ol

    To fully duplicate the functionality of the built-in String#index I need
    to transfer $~ from the local scope to the enclosing scope. Is there any
    way of doing this?

    // Niklas
     
    Niklas Frykholm, Mar 15, 2005
    #1
    1. Advertising

  2. Niklas Frykholm wrote:

    > To fully duplicate the functionality of the built-in String#index I need
    > to transfer $~ from the local scope to the enclosing scope. Is there any
    > way of doing this?


    There's a solution in the ruby-talk:79283 thread that was posted by
    matz, but it's not thread safe. It would be wonderful if we could have a
    proper way of doing this...
     
    Florian Gross, Mar 15, 2005
    #2
    1. Advertising


  3. >> To fully duplicate the functionality of the built-in String#index I
    >> need to transfer $~ from the local scope to the enclosing scope. Is
    >> there any way of doing this?

    >
    >
    > There's a solution in the ruby-talk:79283 thread that was posted by
    > matz, but it's not thread safe. It would be wonderful if we could have
    > a proper way of doing this...


    Ah... interesting... but that technique doesn't really work with
    String#index, since String#index doesn't take a block.

    I think I just have to resign from the idea of making a completely
    transparent String wrapper.

    But it would be nice if this was resolved. In principle, I think it
    should be possible to do the same things from Ruby as from C. I.e. if C
    can set a variable $~ in the caller's scope, it should be possible to do
    that from Ruby as well.

    // Niklas
     
    Niklas Frykholm, Mar 15, 2005
    #3
  4. Thanks for that code, I was lurking reading this thread and have been
    wondering all day how to implement that, because of you I will get a
    good nights sleep.

    pth


    On Wed, 16 Mar 2005 05:34:47 +0900, Florian Gross <> wrote:
    > Niklas Frykholm wrote:
    >
    > > Ah... interesting... but that technique doesn't really work with
    > > String#index, since String#index doesn't take a block.
    > >
    > > I think I just have to resign from the idea of making a completely
    > > transparent String wrapper.
    > >
    > > But it would be nice if this was resolved. In principle, I think it
    > > should be possible to do the same things from Ruby as from C. I.e. if C
    > > can set a variable $~ in the caller's scope, it should be possible to do
    > > that from Ruby as well.

    >
    > Indeed. See attachment, Binding.of_caller.
    >
    >
    >
     
    Patrick Hurley, Mar 15, 2005
    #4
  5. Florian Gross wrote:

    >> In principle, I think it should be possible to do the same things
    >> from Ruby as from C. I.e. if C can set a variable $~ in the caller's
    >> scope, it should be possible to do that from Ruby as well.

    >
    >
    > Indeed. See attachment, Binding.of_caller.



    Nice. Thanks a lot. I will probably be able to put that to use.

    // Niklas
     
    Niklas Frykholm, Mar 17, 2005
    #5
    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. karthikeyavenkat
    Replies:
    2
    Views:
    675
    Bryce
    Mar 17, 2005
  2. Shawn W_
    Replies:
    5
    Views:
    321
    Aldric Giacomoni
    Sep 16, 2009
  3. ngoc
    Replies:
    5
    Views:
    199
    Tad McClellan
    May 11, 2006
  4. Tomasz Chmielewski

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

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    360
    Tomasz Chmielewski
    Mar 4, 2008
  5. Stefan Mueller
    Replies:
    7
    Views:
    214
    Thomas 'PointedEars' Lahn
    Nov 28, 2005
Loading...

Share This Page