Overriding String#[]

Discussion in 'Ruby' started by Lukasz Muziol, Aug 11, 2006.

  1. The generic question would be "does anyone know a decent and complete
    reference to Ruby's syntax?"

    What interests me is the syntax required to define methods like [], []=,
    getting their parameters in between the braces. It's fairly
    inconvienient that String#[] returns a character code, rather than a
    string of one character. Hope it'll get changed soon (to get in line
    with POLS perhaps :)), accomodating my laziness for now I hope to
    override it. How do I define a function named like this?

    Thanks.

    --
    Posted via http://www.ruby-forum.com/.
     
    Lukasz Muziol, Aug 11, 2006
    #1
    1. Advertising

  2. Lukasz Muziol wrote:
    > The generic question would be "does anyone know a decent and complete
    > reference to Ruby's syntax?"
    >
    > What interests me is the syntax required to define methods like [], []=,
    > getting their parameters in between the braces. It's fairly
    > inconvienient that String#[] returns a character code, rather than a
    > string of one character. Hope it'll get changed soon (to get in line
    > with POLS perhaps :)), accomodating my laziness for now I hope to
    > override it. How do I define a function named like this?


    def [](key)
    # ...
    end

    def []=(key, value)
    # ...
    end

    > Thanks.



    --
    Posted via http://www.ruby-forum.com/.
     
    Eero Saynatkari, Aug 11, 2006
    #2
    1. Advertising

  3. On 8/10/06, Lukasz Muziol <> wrote:
    > The generic question would be "does anyone know a decent and complete
    > reference to Ruby's syntax?"
    >
    > What interests me is the syntax required to define methods like [], []=,
    > getting their parameters in between the braces. It's fairly
    > inconvienient that String#[] returns a character code, rather than a
    > string of one character. Hope it'll get changed soon (to get in line
    > with POLS perhaps :)), accomodating my laziness for now I hope to
    > override it. How do I define a function named like this?


    Well the syntax is:

    class String
    def [](arg)
    end
    end

    But, this one seems like a pretty dangerous mod to me, with the
    potential to break lots of things.

    But, keep in mind that while:

    irb(main):001:0> "abc"[1]
    => 98

    you can get the result you want without changing Strings [] method.

    irb(main):002:0> "abc"[1..1]
    => "b"

    --
    Rick DeNatale
    http://talklikeaduck.denhaven2.com/
     
    Rick DeNatale, Aug 11, 2006
    #3
  4. Lukasz Muziol

    Matt Todd Guest

    Should be just like so:

    def [] index
    # handle index or regexp or whatever
    end

    I know that it seems odd, but you basically just put the skeleton of
    the method name in there as the name and then the parameters are what
    go into the expression. You could override the + method (and many
    others) as well. For instance:

    class Foo
    def + operand
    # do some merging or something of whatever Foo is
    end
    end

    In that way, you can define some weird magic to happen, like taking
    two ActiveRecord models in and spitting out a relationship, for
    instance:

    (Post.find_by_id(12) + Tag.find_or_create_by_tag('Ruby')).save

    We call #save on what's returned because it's a PostTag model that's returned...

    Of course, this could be ironed out a bit, but you see the point:
    overload operators with just their names... as simple as

    def + operand
    # magic here
    end

    Etc, etc, etc.

    M.T.

    (Sorry, I'm a bit drowsy, so this might not make total sense.)
     
    Matt Todd, Aug 11, 2006
    #4
  5. Lukasz Muziol

    Mr Pinto Guest

    > But, keep in mind that while:
    >
    > irb(main):001:0> "abc"[1]
    > => 98
    >
    > you can get the result you want without changing Strings [] method.
    >
    > irb(main):002:0> "abc"[1..1]
    > => "b"


    Or:
    irb(main):001:0> str = "abc"[1].chr
    => "b"
    irb(main):002:0> str.class
    => String

    The []= method should already be doing what you want:

    irb(main):001:0> str = "abc"
    => "abc"
    irb(main):002:0> str[1] = "d"
    => "d"
    irb(main):003:0> str
    => "adc"
     
    Mr Pinto, Aug 11, 2006
    #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. sangam via .NET 247

    overriding,shadowing concept

    sangam via .NET 247, Apr 30, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    3,225
    Sanjeeva
    Jun 23, 2004
  2. sourabh
    Replies:
    1
    Views:
    451
    Alok Jain
    May 21, 2004
  3. Valentin Tihomirov
    Replies:
    10
    Views:
    755
    Mike Treseler
    Nov 3, 2003
  4. Replies:
    9
    Views:
    612
    Mike Treseler
    Feb 10, 2005
  5. Mike Dunn

    Overriding __doPostBack

    Mike Dunn, Jul 9, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    929
    vMike
    Jul 9, 2003
Loading...

Share This Page