[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Quickie: Monkey patching Array

Leon Bogaert

5/29/2008 9:51:00 PM

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

8 Answers

Aria Stewart

5/29/2008 9:58:00 PM

0


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.


Mikael Høilund

5/29/2008 10:05:00 PM

0


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?


Siep Korteling

5/29/2008 10:06:00 PM

0

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

Leon Bogaert

5/29/2008 10:18:00 PM

0

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

Mikael Høilund

5/29/2008 10:25:00 PM

0

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


Siep Korteling

5/29/2008 10:29:00 PM

0

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

Leon Bogaert

5/30/2008 7:44:00 AM

0

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

But I couldn't find it.

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

Leon Bogaert

5/30/2008 7:50:00 AM

0

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