[YANQ] - how to create/modify []=

Hi sir Gavin (aka Gavin Sinclair [mailto:gsinclair@soyabean.com.au]):

Your wrote last Monday, December 09, 2002 6:16 PM:

Now this is strange: someone who doesn’t like modifying
objects wants to modify classes instead!

. I thought it wasn’t a big deal. I just want to change behaviour. In
general, I want methods to return copies of objects, not change them. In
Arr#=, I want it to return an array since I see usefulness in it (rather
than returning just the inserted value itself -why return something you know
in advance??).

Anyway, I thought it would be really easy, but I can’t seem to do it.

sirs Guy, Bj, Watanabe, and Martin have posted working copies. Of course, I
do not know yet the repercussions of what I am asking… I am just after all
a nuby :frowning:

Cheers,
Gavin

Many thanks sirs Gavin, Guy, Bj, Watanabe, and Martin for the inputs.

Kind regards,
-botp

Hi –

Hi sir Gavin (aka Gavin Sinclair [mailto:gsinclair@soyabean.com.au]):

Your wrote last Monday, December 09, 2002 6:16 PM:

Now this is strange: someone who doesn’t like modifying
objects wants to modify classes instead!

. I thought it wasn’t a big deal. I just want to change behaviour. In
general, I want methods to return copies of objects, not change them. In
Arr#=, I want it to return an array since I see usefulness in it (rather
than returning just the inserted value itself -why return something you know
in advance??).

You may or may not know it in advance:

arr[0] = gets

Also, what you’re describing is how assignments work: the return value
of the assignment is the right hand side of the expression. True, =
isn’t an assignment, but a method call… But the cool thing about
the = syntax is that it looks and acts just like an assignment.

So, for example, you can do:

a = b = arr[0] = x

which would probably blow up if = no longer did what it looks like
it does (i.e., act like an assignment).

Or, to put it another way: if a method that altered an array element
were going to return the array itself, then there would be much better
things to call it than = .

sirs Guy, Bj, Watanabe, and Martin have posted working copies. Of course, I
do not know yet the repercussions of what I am asking… I am just after all
a nuby :frowning:

If you change Array#= globally, then the main repercussions are that
you can’t safely use any libraries (including core ones), and no one
else can use any libraries or modules you write. So it’s probably not
a great idea :slight_smile: If you do one of the non-global things, like putting
the change in a module and extending individual objects, then it’s
safer and somewhat more sociable.

David

···

On Tue, 10 Dec 2002, [iso-8859-1] “Peña, Botp” wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav