Quickie: Monkey patching Array

you are just assigning a to an array literal
This is equivalent to
a = Array.new(['one','two','three'])
Like Siep said, array access a[0] will raise the error.
r

···

----- Original Message ----
From: Siep Korteling <s.korteling@gmail.com>
To: ruby-talk ML <ruby-talk@ruby-lang.org>
Sent: Thursday, May 29, 2008 3:28:45 PM
Subject: Re: Quickie: Monkey patching Array

Leon Bogaert wrote:

I know it's bad behaviour :slight_smile: 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/\.