Quickie: Monkey patching Array

Discussion in 'Ruby' started by Leon Bogaert, May 29, 2008.

  1. Leon Bogaert

    Leon Bogaert Guest

    Hi all,

    A quick question: is it possible to monkey patch the Array [] method in
    ruby?

    I tried:

    class Array
    def []=(elem)
    raise 'Yesss... It works!'
    end
    end

    But that didn't work. I tried patching the Kernel module but that didn't
    have any effect either. Is the [] hidden else somewhere? Or do I have to
    use rubinius for that? :)

    Thanks!

    Leon
    --
    Posted via http://www.ruby-forum.com/.
    Leon Bogaert, May 29, 2008
    #1
    1. Advertising

  2. Leon Bogaert

    Aria Stewart Guest

    On May 29, 2008, at 3:51 PM, Leon Bogaert wrote:

    > Hi all,
    >
    > A quick question: is it possible to monkey patch the Array [] method
    > in
    > ruby?
    >
    > I tried:
    >
    > class Array
    > def []=(elem)
    > raise 'Yesss... It works!'
    > end
    > end




    Try [], not []=. They're different methods.
    Aria Stewart, May 29, 2008
    #2
    1. Advertising

  3. On May 29, 2008, at 23:51, Leon Bogaert wrote:

    > Hi all,
    >
    > A quick question: is it possible to monkey patch the Array [] method =20=


    > in
    > ruby?
    >
    > I tried:
    >
    > class Array
    > def []=3D(elem)


    You monkey patched the []=3D method, not the [] method. Try
    def [](index)

    Also: Are you sure this is necessary? I can only imagine overwriting =20
    Array#[] can lead to bad things.

    --=20
    # Mikael H=F8ilund
    def method_missing(m, a=3D0) a +
    m.to_s[/[a-z]+/].size * 2; end
    p What is the meaning of life?
    Mikael Høilund, May 29, 2008
    #3
  4. Leon Bogaert wrote:
    > Hi all,
    >
    > A quick question: is it possible to monkey patch the Array [] method in
    > ruby?
    >
    > I tried:
    >
    > class Array
    > def []=(elem)
    > raise 'Yesss... It works!'
    > end
    > end
    >
    > But that didn't work. I tried patching the Kernel module but that didn't
    > have any effect either. Is the [] hidden else somewhere? Or do I have to
    > use rubinius for that? :)
    >
    > Thanks!
    >
    > Leon


    You redefined the []= method, not the [] method.
    class Array
    def [](elem) # just get rid of the "="
    raise 'Yesss... It works!'
    end
    end

    groeten,

    Siep

    --
    Posted via http://www.ruby-forum.com/.
    Siep Korteling, May 29, 2008
    #4
  5. Leon Bogaert

    Leon Bogaert Guest

    I know it's bad behaviour :) But I'm just fiddling with ruby.

    class Array
    def [](elem) # just get rid of the "="
    raise 'Yesss... It works!'
    end
    end

    a = ['one', 'two', 'three']
    p a

    Didn't work also. It just prints: ["one", "two", "three"]
    --
    Posted via http://www.ruby-forum.com/.
    Leon Bogaert, May 29, 2008
    #5
  6. On May 30, 2008, at 0:18, Leon Bogaert wrote:

    > class Array
    > def [](elem) # just get rid of the "=3D"
    > raise 'Yesss... It works!'
    > end
    > end
    >
    > a =3D ['one', 'two', 'three']


    That's an array literal, not Array#[]. No way to overload that, I'm =20
    afraid. Try running:
    a[0]

    --=20
    Name =3D "Mikael H=F8ilund"; Email =3D Name.gsub %r/\s/,%#=3D?,# ## =
    visit
    *a=3De=3D?=3D,!????,:??,?,,Email.downcase![eval(%["\\%o\\%o"]% ## =
    http://
    [?**2+?o,?\\*2])]=3D"o";Email.gsub! %%\%c%*3%a, %?%c? % ?@ ## hoilund
    def The(s)%%\%s.%%s+%.org\n.end; :Go and print The Email ## dot org
    Mikael Høilund, May 29, 2008
    #6
  7. Leon Bogaert wrote:
    > I know it's bad behaviour :) But I'm just fiddling with ruby.
    >
    > class Array
    > def [](elem) # just get rid of the "="
    > raise 'Yesss... It works!'
    > end
    > end
    >
    > a = ['one', 'two', 'three']
    > p a
    >
    > Didn't work also. It just prints: ["one", "two", "three"]


    try

    p a[0]


    [] is just a method. You chanced it. To verify if your change works, you
    'll have to use the [] method. If this is not what you want, what
    outcome did you expect?

    regards,

    Siep
    --
    Posted via http://www.ruby-forum.com/.
    Siep Korteling, May 29, 2008
    #7
  8. Leon Bogaert

    Leon Bogaert Guest

    Leon Bogaert, May 30, 2008
    #8
  9. Leon Bogaert

    Leon Bogaert Guest

    Ah, I read the post about the array literal.

    Thanks for the replies! I'll try and make it work another way .

    Leon
    --
    Posted via http://www.ruby-forum.com/.
    Leon Bogaert, May 30, 2008
    #9
    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. gentlestone

    monkey patching with @classmethod

    gentlestone, Mar 3, 2010, in forum: Python
    Replies:
    3
    Views:
    543
    gentlestone
    Mar 3, 2010
  2. Jason

    Subclassing by monkey-patching

    Jason, Sep 5, 2010, in forum: Python
    Replies:
    8
    Views:
    391
    Glazner
    Sep 6, 2010
  3. John Lam
    Replies:
    0
    Views:
    87
    John Lam
    Sep 15, 2006
  4. S. Robert James

    Monkey Patching a method and back

    S. Robert James, Dec 10, 2006, in forum: Ruby
    Replies:
    9
    Views:
    122
    Martin DeMello
    Dec 11, 2006
  5. Wes Gamble

    Monkey patching a private method?

    Wes Gamble, Dec 19, 2007, in forum: Ruby
    Replies:
    5
    Views:
    100
    Chris Cummer
    Dec 20, 2007
Loading...

Share This Page