Need #collect! on partial array

Hi:

Recently I needed to modify an array.
The array was part of a hash element, so
it had to be modified in place.

#collect! would have been a great way to go,
but I did not need to modify the whole array.

For example

a=%w{one two three}

a.collect! { |i| i.upcase }

will modify the whole array.

a[1…-1].collect! { |i| i.upcase }

will modify the anonymous array created by a[1…-1],
but not a.

This could have been solved with #collect_with_index!
but AFAIK, this does not exist.

Is there another way to achieve this same affect?

Thanks

···


Jim Freeze

Intolerance is the last defense of the insecure.

Hi –

Hi:

Recently I needed to modify an array.
The array was part of a hash element, so
it had to be modified in place.

#collect! would have been a great way to go,
but I did not need to modify the whole array.

For example

a=%w{one two three}

a.collect! { |i| i.upcase }

will modify the whole array.

a[1…-1].collect! { |i| i.upcase }

will modify the anonymous array created by a[1…-1],
but not a.

This could have been solved with #collect_with_index!
but AFAIK, this does not exist.

Citizens for MWI [map_with_index] welcomes you to its ranks :slight_smile: But I
think it’s been rejected.

Is there another way to achieve this same affect?

You could do some variant of:

(0…a.size).each {|i| <do something, or not, with a[i]>}

David

···

On Sat, 29 Mar 2003, Jim Freeze wrote:


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

In article 20030328164026.A42337@freeze.org,

Hi:

Recently I needed to modify an array.
The array was part of a hash element, so
it had to be modified in place.

#collect! would have been a great way to go,
but I did not need to modify the whole array.

For example

a=%w{one two three}

a.collect! { |i| i.upcase }

will modify the whole array.

a[1…-1].collect! { |i| i.upcase }

will modify the anonymous array created by a[1…-1],
but not a.

This could have been solved with #collect_with_index!
but AFAIK, this does not exist.

Is there another way to achieve this same affect?

[mike@won mike]$ irb
irb(main):001:0> a = %w{one two three}
[“one”, “two”, “three”]
irb(main):002:0> a[1 … -1].collect! { |i| i.upcase! }
[“TWO”, “THREE”]
irb(main):003:0> a
[“one”, “TWO”, “THREE”]
irb(main):004:0>

Mike

···

Jim Freeze jim@freeze.org wrote:

mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA

In article 20030328164026.A42337@freeze.org,

Hi:

Recently I needed to modify an array.
The array was part of a hash element, so
it had to be modified in place.

#collect! would have been a great way to go,
but I did not need to modify the whole array.

For example

a=%w{one two three}

a.collect! { |i| i.upcase }

will modify the whole array.

a[1…-1].collect! { |i| i.upcase }

will modify the anonymous array created by a[1…-1],
but not a.

This could have been solved with #collect_with_index!
but AFAIK, this does not exist.

Is there another way to achieve this same affect?

[mike@won mike]$ irb
irb(main):001:0> a = %w{one two three}
[“one”, “two”, “three”]
irb(main):002:0> a[1 … -1].collect! { |i| i.upcase! }

It is cheating, in a way ;-). `i’ holds a reference to the original element
and your ‘upcase!’ modifies it in place. You can replace ‘collect!’ with
‘each’ and the result will be the same, in other words ‘collect!’ does not
work here at all (to be precise, it modifies an array that will be marked
for GC immediately). As far as I understand it is not what the original post
meant.

[“TWO”, “THREE”]
irb(main):003:0> a
[“one”, “TWO”, “THREE”]
irb(main):004:0>

Mike

mike@stok.co.uk | The “`Stok’ disclaimers”
apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28
3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599
13DA

···

----- Original Message -----
From: “Mike Stok” mike@ratdog.stok.co.uk
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, March 28, 2003 2:01 PM
Subject: Re: Need #collect! on partial array

Jim Freeze jim@freeze.org wrote:

Hi –

Citizens for MWI [map_with_index] welcomes you to its ranks :slight_smile: But I
think it’s been rejected.

Well, it’s nice to finally find a group that welcomes me. :slight_smile:
It’s just my luck that the group has been disbanded even
before I was invited to be a member.

Is there another way to achieve this same affect?

You could do some variant of:

(0…a.size).each {|i| <do something, or not, with a[i]>}

I will probably end up with this construct.

···

On Saturday, 29 March 2003 at 6:53:34 +0900, dblack@superlink.net wrote:


Jim Freeze

“I refuse to have a battle of wits with an unarmed person.”

It is cheating, in a way ;-). `i’ holds a reference to the original
element
and your ‘upcase!’ modifies it in place. You can replace ‘collect!’ with
‘each’ and the result will be the same, in other words ‘collect!’ does not
work here at all (to be precise, it modifies an array that will be marked
for GC immediately). As far as I understand it is not what the original
post
meant.

Right… for example, if instead of uppercasing
some strings you were incrementing some
integers, this wouldn’t work.

Hal

···

----- Original Message -----
From: “Gennady” gfb@tonesoft.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, March 28, 2003 4:23 PM
Subject: Re: Need #collect! on partial array