Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Deleting values of array elements

Reply
Thread Tools

Deleting values of array elements

 
 
Jonas Schneider
Guest
Posts: n/a
 
      02-11-2008
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', rop => '123'},
{:a => 'blab', rop => '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
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      02-11-2008
On 11.02.2008 17:52, Jonas Schneider wrote:
> 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', rop => '123'},
> {:a => 'blab', rop => '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', rop => '123'},
irb(main):003:1* {:a => 'blab', rop => '234'}
irb(main):004:1> ]
=> [{:a=>"blah", rop=>"123"}, {:a=>"blab", rop=>"234"}]
irb(main):005:0> a.each {|x| x.delete rop}
=> [{:a=>"blah"}, {:a=>"blab"}]

?

If you want the original unmodified you can do

a.map {|x| x.dup.delete rop}

Cheers

robert

 
Reply With Quote
 
 
 
 
Jonas Schneider
Guest
Posts: n/a
 
      02-11-2008
On 11 Feb., 18:07, Robert Klemme <shortcut...@googlemail.com> wrote:
> a.map {|x| x.dup.delete rop}


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
 
Reply With Quote
 
Rick DeNatale
Guest
Posts: n/a
 
      02-11-2008
On 2/11/08, Jonas Schneider <> wrote:
> On 11 Feb., 18:07, Robert Klemme <shortcut...@googlemail.com> wrote:
> > a.map {|x| x.dup.delete rop}

>
> 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?


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/

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      02-11-2008
On 11.02.2008 21:54, Jonas Schneider wrote:
> On 11 Feb., 18:07, Robert Klemme <shortcut...@googlemail.com> 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
 
Reply With Quote
 
Jonas Schneider
Guest
Posts: n/a
 
      02-12-2008
On 11 Feb., 22:23, Robert Klemme <shortcut...@googlemail.com> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting data from the file without deleting the file first crea C++ 2 12-28-2012 11:50 PM
Looping through array, deleting elements dkmd_nielsen Ruby 5 03-08-2007 09:42 PM
XML elements to JavaScript Array elements Conversion P XML 1 07-07-2006 09:08 PM
Deleting a File from Hardrive and Deleting a SubKey in Registry Harry Barker C++ 2 04-19-2006 09:34 AM
Adding and deleting elements from a Textbox Array Newcomsas Javascript 3 12-01-2005 11:50 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57