Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Quickie: Monkey patching Array

Reply
Thread Tools

Quickie: Monkey patching Array

 
 
Leon Bogaert
Guest
Posts: n/a
 
      05-29-2008
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/.

 
Reply With Quote
 
 
 
 
Aria Stewart
Guest
Posts: n/a
 
      05-29-2008

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.


 
Reply With Quote
 
 
 
 
Mikael H°ilund
Guest
Posts: n/a
 
      05-29-2008

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?


 
Reply With Quote
 
Siep Korteling
Guest
Posts: n/a
 
      05-29-2008
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/.

 
Reply With Quote
 
Leon Bogaert
Guest
Posts: n/a
 
      05-29-2008
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/.

 
Reply With Quote
 
Mikael H°ilund
Guest
Posts: n/a
 
      05-29-2008
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


 
Reply With Quote
 
Siep Korteling
Guest
Posts: n/a
 
      05-29-2008
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/.

 
Reply With Quote
 
Leon Bogaert
Guest
Posts: n/a
 
      05-30-2008
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/.

 
Reply With Quote
 
Leon Bogaert
Guest
Posts: n/a
 
      05-30-2008
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Subclassing by monkey-patching Jason Python 8 09-06-2010 10:40 AM
monkey patching with @classmethod gentlestone Python 3 03-03-2010 01:45 PM
Monkey patching a private method? Wes Gamble Ruby 5 12-20-2007 01:20 PM
Monkey Patching a method and back S. Robert James Ruby 9 12-11-2006 11:03 AM
monkey-patching const_missing and friends John Lam Ruby 0 09-15-2006 04:05 PM



Advertisments