[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Quickie: Monkey patching Array

Reidmix

5/29/2008 11:56:00 PM

[Note: parts of this message were removed to make it a legal post.]

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