Quickie: Monkey patching Array

L

Leon Bogaert

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
 
A

Aria Stewart

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

Mikael Høilund

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?
 
S

Siep Korteling

Leon said:
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
 
L

Leon Bogaert

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"]
 
M

Mikael Høilund

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
 
S

Siep Korteling

Leon said:
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
 
L

Leon Bogaert

Ah, I read the post about the array literal.

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

Leon
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top