# Deleting values of array elements

**URL:** <https://www.ruby-forum.com/t/deleting-values-of-array-elements/128802>\
**Category:** Ruby\
**Created:** [February 11, 2008, 5:55pm UTC](https://www.ruby-forum.com/t/deleting-values-of-array-elements/128802 "2008-02-11T17:55:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonas\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jonas\_s](https://www.ruby-forum.com/u/jonas_s)\
**Post date:** [February 11, 2008, 5:55pm UTC](https://www.ruby-forum.com/t/deleting-values-of-array-elements/128802/1 "2008-02-11T17:55:37Z")

</div>

Heya,

I’m pretty sure theres an easy way for this:  
I have a collection of objects; I want to remove 1 property (they all  
have it in common) from them.

so if

a = [  
{:a =\> ‘blah’, :prop =\> ‘123’},  
{:a =\> ‘blab’, :prop =\> ‘234’}  
]

I want to remove the attribute “prop” from all of them.  
I tried it with this the first time:

a.collect {|x| y = x.to\_a; y.delete(x.prop); y }

but it doesnt seem to work; i the property isn’t removed, and it would  
also be bad if some other property would have the same value cause it  
would get removed, too.

I´m sure there’s some a.without method or something, but I’m not able  
to find it…

Help?

Greets  
Jonas

---

<div class="post-metadata">

**Author:** ![jonas\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jonas\_s](https://www.ruby-forum.com/u/jonas_s)\
**Post date:** [February 11, 2008, 6:11pm UTC](https://www.ruby-forum.com/t/deleting-values-of-array-elements/128802/2 "2008-02-11T18:11:08Z")

</div>

On 11.02.2008 17:52, Jonas S. wrote:

> {:a =\> ‘blab’, :prop =\> ‘234’}  
> ]
> 
> I want to remove the attribute “prop” from all of them.

That is not an attribute but a hash key.

> I tried it with this the first time:
> 
> a.collect {|x| y = x.to\_a; y.delete(x.prop); y }
> 
> but it doesnt seem to work; i the property isn’t removed, and it would  
> also be bad if some other property would have the same value cause it  
> would get removed, too.
> 
> I´m sure there’s some a.without method or something, but I’m not able  
> to find it…

Do you mean

irb(main):001:0\> a = [  
irb(main):002:1\* {:a =\> ‘blah’, :prop =\> ‘123’},  
irb(main):003:1\* {:a =\> ‘blab’, :prop =\> ‘234’}  
irb(main):004:1\> ]  
=\> [{:a=\>“blah”, :prop=\>“123”}, {:a=\>“blab”, :prop=\>“234”}]  
irb(main):005:0\> a.each {|x| x.delete :prop}  
=\> [{:a=\>“blah”}, {:a=\>“blab”}]

?

If you want the original unmodified you can do

a.map {|x| x.dup.delete :prop}

Cheers

robert

---

<div class="post-metadata">

**Author:** ![jonas\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jonas\_s](https://www.ruby-forum.com/u/jonas_s)\
**Post date:** [February 11, 2008, 10:21pm UTC](https://www.ruby-forum.com/t/deleting-values-of-array-elements/128802/3 "2008-02-11T22:21:40Z")

</div>

On 2/11/08, Jonas S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> aequivalent in ruby to get rid of it completely?  
> Not as far as I know in Ruby \< 1.9

Ruby 1.9 has a private Object#remove\_instance\_variable method, but  
unless you are using the defined? operator you really can’t tell  
whether an instance variable exists since just referring to it creates  
it if it doesn’t already exist.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![jonas\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jonas\_s](https://www.ruby-forum.com/u/jonas_s)\
**Post date:** [February 11, 2008, 10:26pm UTC](https://www.ruby-forum.com/t/deleting-values-of-array-elements/128802/4 "2008-02-11T22:26:22Z")

</div>

On 11.02.2008 21:54, Jonas S. wrote:

> On 11 Feb., 18:07, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> only problem: The array actually consists of objects, I forgot.

So the code you showed is not the original code?

> So they dont have the delete method.  
> I’ve now done this with _x.dup.prop = nil; x_ but it seems a little  
> ugly to have that nil still in there.  
> I know, in PHP there is the unset function; is there somethin  
> aequivalent in ruby to get rid of it completely?

Why don’t you

a.map {|x| x = x.dup; x.prop = nil; x}

? Do you actually need the copy?

robert

---

<div class="post-metadata">

**Author:** ![jonas\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jonas\_s](https://www.ruby-forum.com/u/jonas_s)\
**Post date:** [February 11, 2008, 9:55pm UTC](https://www.ruby-forum.com/t/deleting-values-of-array-elements/128802/5 "2008-02-11T21:55:48Z")

</div>

On 11 Feb., 18:07, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> a.map {|x| x.dup.delete :prop}

yay, works great =)

only problem: The array actually consists of objects, I forgot.  
So they dont have the delete method.  
I’ve now done this with _x.dup.prop = nil; x_ but it seems a little  
ugly to have that nil still in there.  
I know, in PHP there is the unset function; is there somethin  
aequivalent in ruby to get rid of it completely?

Greets  
Jonas

---

<div class="post-metadata">

**Author:** ![jonas\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jonas\_s](https://www.ruby-forum.com/u/jonas_s)\
**Post date:** [February 12, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/deleting-values-of-array-elements/128802/6 "2008-02-12T16:05:28Z")

</div>

On 11 Feb., 22:23, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So the code you showed is not the original code?

Nah, I simplified it =)

I now have it done with x.clone(), so all works great now.

I didn’t use defined?, but to\_json, and I didnt want it to appear  
there.

Thanks =)  
– Jonas
