Quickie: Monkey patching Array

  • Follow


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/.

0
Reply leon160 (34) 5/29/2008 9:51:05 PM

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.


0
Reply aredridel (268) 5/29/2008 9:58:25 PM


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?


0
Reply mikael3164 (46) 5/29/2008 10:05:19 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
> 
> 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/.

0
Reply s.korteling (175) 5/29/2008 10:05:40 PM

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/.

0
Reply leon160 (34) 5/29/2008 10:18:01 PM

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


0
Reply mikael3164 (46) 5/29/2008 10:25:03 PM

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/.

0
Reply s.korteling (175) 5/29/2008 10:28:45 PM

Hee Siep,

Well, actually I tried to change the [] method with which you create 
arrays. This one: ['one', 'two', 'three']

I tried looking if it exists in the ruby kernel: 
http://www.ruby-doc.org/core/classes/Kernel.html

But I couldn't find it.

Leon
-- 
Posted via http://www.ruby-forum.com/.

0
Reply leon160 (34) 5/30/2008 7:43:57 AM

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/.

0
Reply leon160 (34) 5/30/2008 7:49:56 AM

8 Replies
33 Views

(page loaded in 0.171 seconds)


Reply: